admin管理员组文章数量:1023242
Consider the following:
x = function () {}
p = new x()
console.log(p) // ok
Math.z = function () {}
p = new Math.z()
console.log(p) // ok
p = new Math.round()
console.log(p) // TypeError: function round() { [native code] } is not a constructor
So I can use new
with my own functions, but not with Math.round
. What makes it so special? Is this documented somewhere?
Consider the following:
x = function () {}
p = new x()
console.log(p) // ok
Math.z = function () {}
p = new Math.z()
console.log(p) // ok
p = new Math.round()
console.log(p) // TypeError: function round() { [native code] } is not a constructor
So I can use new
with my own functions, but not with Math.round
. What makes it so special? Is this documented somewhere?
3 Answers
Reset to default 8It's nothing special about Math.round
You can replicate this behaviour on your own functions:
MyClass = function(){};
MyClass.round = function(x){
if(this instanceof MyClass.round)
throw 'TypeError: MyClass.round is not a constructor';
return Math.round(x);
}
console.log(MyClass.round(0.5)); // 1
new MyClass.round(); // 'TypeError: MyClass.round is not a constructor'
In fact you can use a similar pattern to make the new
keyword optional on your Class:
function MyClass(){
if(!(this instanceof MyClass)) // If not using new
return new MyClass(); // Create a new instance and return it
// Do normal constructor stuff
this.x = 5;
}
(new MyClass()).x === MyClass().x;
As to why new
doesn't work with built-in functions and methods, this is by design, and documented:
None of the built-in functions described in this clause that are not constructors shall implement the [[Construct]] internal method unless otherwise specified in the description of a particular function. -- http://es5.github./#x15
It is a method, that's why you can not put new
before it.
ps:
new alert() //TypeError: Illegal invocation
new console.log() //TypeError: Illegal invocation
new function(){} //OK
Math is a class, so you can create an object from it. Math.round is a method of Math. You cannot create an object from a method.
Consider the following:
x = function () {}
p = new x()
console.log(p) // ok
Math.z = function () {}
p = new Math.z()
console.log(p) // ok
p = new Math.round()
console.log(p) // TypeError: function round() { [native code] } is not a constructor
So I can use new
with my own functions, but not with Math.round
. What makes it so special? Is this documented somewhere?
Consider the following:
x = function () {}
p = new x()
console.log(p) // ok
Math.z = function () {}
p = new Math.z()
console.log(p) // ok
p = new Math.round()
console.log(p) // TypeError: function round() { [native code] } is not a constructor
So I can use new
with my own functions, but not with Math.round
. What makes it so special? Is this documented somewhere?
3 Answers
Reset to default 8It's nothing special about Math.round
You can replicate this behaviour on your own functions:
MyClass = function(){};
MyClass.round = function(x){
if(this instanceof MyClass.round)
throw 'TypeError: MyClass.round is not a constructor';
return Math.round(x);
}
console.log(MyClass.round(0.5)); // 1
new MyClass.round(); // 'TypeError: MyClass.round is not a constructor'
In fact you can use a similar pattern to make the new
keyword optional on your Class:
function MyClass(){
if(!(this instanceof MyClass)) // If not using new
return new MyClass(); // Create a new instance and return it
// Do normal constructor stuff
this.x = 5;
}
(new MyClass()).x === MyClass().x;
As to why new
doesn't work with built-in functions and methods, this is by design, and documented:
None of the built-in functions described in this clause that are not constructors shall implement the [[Construct]] internal method unless otherwise specified in the description of a particular function. -- http://es5.github./#x15
It is a method, that's why you can not put new
before it.
ps:
new alert() //TypeError: Illegal invocation
new console.log() //TypeError: Illegal invocation
new function(){} //OK
Math is a class, so you can create an object from it. Math.round is a method of Math. You cannot create an object from a method.
本文标签: javascriptMathround is not a constructorStack Overflow
版权声明:本文标题:javascript - Math.round is not a constructor - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745523911a2154437.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论