最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does `Object()` constructor work when implementing `Function.call`? - Stack Overflow

programmeradmin1浏览0评论

I was trying to implement the Function.call without using call in any form. The idea was to create an object whose this is the value passed as thisArg to the call method. Initially Object.create(thisArg) came to mind, however that doesn't really work since it only sets the prototype of the object to thisArg. The answer basically boils down to using Object() constructor, and then setting the function with arguments as a 'method' on some key for that object.

Something along this lines:

 thisArg = thisArg || window;
 thisArg = Object(thisArg); 

 const methodSymbol = Symbol();
 thisArg[methodSymbol] = this; // this is the function on which call is executed.

My query is that I am a bit confused on the working of the Object constructor - it seems it sets the this of the object method to the value passed in the constructor, but there is some other malarkey in the spec/MDN docs that basically confuses me. Is there a better explanation on why this works? any other way to implement the same? maybe this is some hacky JS workings that I am not aware of.

I was trying to implement the Function.call without using call in any form. The idea was to create an object whose this is the value passed as thisArg to the call method. Initially Object.create(thisArg) came to mind, however that doesn't really work since it only sets the prototype of the object to thisArg. The answer basically boils down to using Object() constructor, and then setting the function with arguments as a 'method' on some key for that object.

Something along this lines:

 thisArg = thisArg || window;
 thisArg = Object(thisArg); 

 const methodSymbol = Symbol();
 thisArg[methodSymbol] = this; // this is the function on which call is executed.

My query is that I am a bit confused on the working of the Object constructor - it seems it sets the this of the object method to the value passed in the constructor, but there is some other malarkey in the spec/MDN docs that basically confuses me. Is there a better explanation on why this works? any other way to implement the same? maybe this is some hacky JS workings that I am not aware of.

Share Improve this question asked Feb 3 at 13:39 devsawdevsaw 1,0473 gold badges14 silver badges28 bronze badges 2
  • Your question is not clear. Exactly what behavior is confusing? An sample of code and an explanation of what you expect versus what actually happens would be very helpful. – Pointy Commented Feb 3 at 13:45
  • I was expecting the Object constructor to take in the field like a typical OOP constructor. Apparently, if we pass in an object, that is returned as is as an object. I think it makes a little more sense when I read the spec again. – devsaw Commented Feb 3 at 15:32
Add a comment  | 

1 Answer 1

Reset to default 2

The Object constructor when called without new will

  • given an object, returns the same object
  • given null or undefined, returns a plain object (like calling new Object() or just using {})
  • given a primitive, returns the object wrapper for the primitive

console.log( typeof Object("hello") );            // "object"
console.log( Object("hello") instanceof String ); // true

console.log( typeof Object(42) );            // "object"
console.log( Object(42) instanceof Number ); // true

The object constructor without new is used when you absolutely need an object, rather than any other primitive. For the line thisArg[methodSymbol] = this; you cannot use it with primitives, because you cannot change any of their properties. But you can if the value was capital String, Number, Boolean, etc.

发布评论

评论列表(0)

  1. 暂无评论