[英]Why functions don't use new keyword while being declared in constructors?
sample function declaration in function : 函数中的示例函数声明:
function Rectangle(height, width) {
this.height = height;
this.width = width;
this.calcArea = function() {
return this.height * this.width;
};
// put our perimeter function here!
this.calcPerimeter = function() {
return 2 * this.height + 2 * this.width;
};
sample new function declaration : 示例新函数声明:
var actions = new function() {
console.log("this is to do something");
}
Why are we using new keyword while declaring a new function but not using new keyword while declaring it in a constructor?? 为什么我们在声明新函数时使用new关键字,而在构造函数中声明它时却不使用new关键字?
new
is an operator. new
是一个运算符。 It allocates an object instance (and assigns it a prototype). 它分配一个对象实例(并为其分配一个原型)。 The functions you create aren't instances of any sort of class, and are not going to be created in multiple copies. 您创建的函数不是任何类的实例,也不会在多个副本中创建。 The static declaration defines the only instance that will exist. 静态声明定义了将存在的唯一实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.