what effect does the 'new' have in a JS method?
This question already has an answer here:
methodA
is not a method, because the new
operator causes the function after it to be called as a constructor. So you get back an object for methodA
with the anonymous function as the equivalent of its class.
It's as if you had written this:
var MethodA = function() {
alert('a');
};
this.methodA = new MethodA;
And that last line is the same as this:
this.methodA = new MethodA();
New is used on functions to create objects. These functions are constructors. As function creates functions, new function creates objects. When you use an anonymous function you create an object of kind "object". When you specify the name of the constructor function, you create an object of that kind: eg
function Human(){};
man=new Human();
man is of the kind "human", or better is an instance of Human:
man instanceof Human
链接地址: http://www.djcxy.com/p/40860.html上一篇: Javascript [this]与新的关键字绑定
下一篇: “新”对JS方法有什么影响?