admin管理员组文章数量:1391943
In a constructor object in JS if it's created without the new keyword the 'this' variable is a reference to the window instead of the object. Is this a particularly wise idea to try and detect that?
function MyObject ()
{
if (this === window) {
alert('[ERROR] Oopsy! You seem to have forgotten to use the "new" keyword.');
return;
}
// Continue with the constructor...
}
// Call the constructor without the new keyword
obj = MyObject()
In a constructor object in JS if it's created without the new keyword the 'this' variable is a reference to the window instead of the object. Is this a particularly wise idea to try and detect that?
function MyObject ()
{
if (this === window) {
alert('[ERROR] Oopsy! You seem to have forgotten to use the "new" keyword.');
return;
}
// Continue with the constructor...
}
// Call the constructor without the new keyword
obj = MyObject()
Share
Improve this question
asked Nov 27, 2013 at 19:00
RichardRichard
5,1034 gold badges31 silver badges48 bronze badges
6
- 1 possible duplicate of How to detect if a function is called as constructor? – Oriol Commented Nov 27, 2013 at 19:04
-
Your code doesn't detect things like
var obj = MyObject.call(foo)
– Oriol Commented Nov 27, 2013 at 19:06 - Sorry, why would you want to? Describe the preferred usage in your documentation, but it is ultimately up to the developer as to how they want to use/implement your object. That's the beauty of programming/scripting. – zamnuts Commented Nov 27, 2013 at 19:18
- @zamnuts there are many cases where you want to be sure you were called with new. For example, if MyObject has a name, and you go this.name = 'Fred', you just wiped out window.name if this wasn't called with a new. Not want you wanted and will probably cause a bug. – user949300 Commented Nov 27, 2013 at 19:27
- @user949300 Whose fault is that? The implementor's. IMO, it is one thing to clean up after "stupid" users (end-user/consumer), it is another thing to pensate for inpetent developers. From the POV of an advanced dev: You're telling me I can't use the module you've developed in a manner that suites me? Fine, I'll refactor your lib or roll my own, and either case send you hate mail for wasting my time. – zamnuts Commented Nov 27, 2013 at 19:36
3 Answers
Reset to default 8It is fairly mon to force a new object when the keyword is omitted-
function Group(O){
if(!(this instanceof Group)) return new Group(O);
if(!O || typeof O!= 'object') O= {};
for(var p in O){
if(O.hasOwnProperty(p)) this[p]= O[p];
}
}
The new.target
property was designed specifically for this purpose:
function Foo() {
if (!new.target) { throw 'Foo() must be called with new'; }
}
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/new.target
I found this in SO, the last answer uses your approach;
Two ways, essentially the same under the hood. You can test what the scope of this is or you can test what this.constructor is.
but the accepted one says no reliable way to do that;
How to detect if a function is called as constructor?
EDIT.
In the book Javascript Enlightment mentions that not necesarily will be window but the parent object;
On the other hand, if you create a constructor function and call it without the use of the new keyword the this value will refer to the “parent” object that contains the function.
本文标签: javascriptDetecting if the new keyword was used inside the constructorStack Overflow
版权声明:本文标题:javascript - Detecting if the new keyword was used inside the constructor? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744675985a2619112.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论