Something went wrong. Try again.
javascript for 🐜's, a tiny runtime with big ambitions antjs.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687// Test Function.prototype.call, apply, and bind methods
// Test 1: Basic call with no thisfunction greet(name) { return "Hello, " + name; }console.log("Test 1 - call:", greet.call(null, "World")); // Hello, World
// Test 2: call with custom thisfunction showThis() { return this.value; }let obj = { value: 42 };console.log("Test 2 - call with this:", showThis.call(obj)); // 42
// Test 3: call with multiple argumentsfunction add(a, b, c) { return a + b + c; }console.log("Test 3 - call multiple args:", add.call(null, 1, 2, 3)); // 6
// Test 4: apply with array of argumentsfunction sum(a, b, c) { return a + b + c; }console.log("Test 4 - apply:", sum.apply(null, [10, 20, 30])); // 60
// Test 5: apply with custom thisfunction getInfo() { return this.name + " is " + this.age; }let person = { name: "Alice", age: 30 };console.log("Test 5 - apply with this:", getInfo.apply(person, [])); // Alice is 30
// Test 6: bind creates new function with bound thisfunction getValue() { return this.x; }let data = { x: 100 };let boundFn = getValue.bind(data);console.log("Test 6 - bind:", boundFn()); // 100
// Test 7: bound function ignores call-site thislet other = { x: 999 };other.fn = boundFn;console.log("Test 7 - bound ignores new this:", other.fn()); // 100
// Test 8: call with undefined this (should work)function returnArg(x) { return x * 2; }console.log("Test 8 - call undefined this:", returnArg.call(undefined, 5)); // 10
// Test 9: Method borrowing with calllet arr1 = { 0: "a", 1: "b", length: 2 };function joinItems() { let result = ""; for (let i = 0; i < this.length; i++) { if (i > 0) result = result + ","; result = result + this[i]; } return result;}console.log("Test 9 - method borrowing:", joinItems.call(arr1)); // a,b
// Test 10: Chained function operationsfunction multiply(factor) { return this.base * factor; }let calc = { base: 5 };console.log("Test 10 - chained:", multiply.call(calc, 3)); // 15
// Test 11: Reflect.apply with arguments object from a class methodclass Forwarder { invoke() { return Reflect.apply(targetMethod, receiver, arguments); }}let receiver = { prefix: "sum" };function targetMethod(a, b, c) { return this.prefix + ":" + [a, b, c].join(",");}console.log("Test 11 - Reflect.apply(arguments):", new Forwarder().invoke(1, 2, 3)); // sum:1,2,3
// Test 12: Reflect.apply with omitted trailing args preserved by arguments lengthclass ActiveSpanLike { startActiveSpan(name, options, callback) { return Reflect.apply(targetStartActiveSpan, this, arguments); }}function targetStartActiveSpan(name, options, callback) { return { argc: arguments.length, name, hasCallback: typeof callback === "function" };}let activeSpanResult = new ActiveSpanLike().startActiveSpan("plugin", { enabled: true }, () => {});console.log( "Test 12 - Reflect.apply method args:", activeSpanResult.argc, activeSpanResult.name, activeSpanResult.hasCallback); // 3 plugin true
console.log("\nAll Function.prototype.call/apply/bind tests completed!");