JavaScript中的新关键字在第一次遇到时可能会非常令人困惑,因为人们倾向于认为JavaScript不是一种面向对象的编程语言。

这是怎么一回事?它解决了什么问题?什么时候合适,什么时候不合适?


当前回答

它做5件事:

它将创建一个新对象。此对象的类型只是对象。它将此新对象的内部、不可访问的[[prototype]](即__proto__)属性设置为构造函数的外部、可访问的原型对象(每个函数对象都自动具有原型属性)。它使该变量指向新创建的对象。每当提到这一点时,它都会使用新创建的对象来执行构造函数。它返回新创建的对象,除非构造函数返回非空对象引用。在这种情况下,将返回该对象引用。

注意:构造函数是指new关键字之后的函数,如

new ConstructorFunction(arg1, arg2)

完成此操作后,如果请求了新对象的未定义属性,脚本将检查对象的[[prototype]]对象的属性。这就是如何在JavaScript中获得类似于传统类继承的东西。

这方面最困难的部分是第2点。每个对象(包括函数)都有一个名为[[prototype]]的内部属性。只能在对象创建时使用new、object.create或基于文本(函数默认为Function.prototype,数字默认为Number.prototype等)设置它。只能使用object.getPrototypeOf(someObject)读取它。没有其他方法设置或读取此值。

除了隐藏的[[prototype]]属性外,函数还有一个名为prototype的属性,您可以通过它访问和修改,为所创建的对象提供继承的财产和方法。


下面是一个示例:

ObjMaker = function() {this.a = 'first';};
// ObjMaker is just a function, there's nothing special about it that makes 
// it a constructor.

ObjMaker.prototype.b = 'second';
// like all functions, ObjMaker has an accessible prototype property that 
// we can alter. I just added a property called 'b' to it. Like 
// all objects, ObjMaker also has an inaccessible [[prototype]] property
// that we can't do anything with

obj1 = new ObjMaker();
// 3 things just happened.
// A new, empty object was created called obj1.  At first obj1 was the same
// as {}. The [[prototype]] property of obj1 was then set to the current
// object value of the ObjMaker.prototype (if ObjMaker.prototype is later
// assigned a new object value, obj1's [[prototype]] will not change, but you
// can alter the properties of ObjMaker.prototype to add to both the
// prototype and [[prototype]]). The ObjMaker function was executed, with
// obj1 in place of this... so obj1.a was set to 'first'.

obj1.a;
// returns 'first'
obj1.b;
// obj1 doesn't have a property called 'b', so JavaScript checks 
// its [[prototype]]. Its [[prototype]] is the same as ObjMaker.prototype
// ObjMaker.prototype has a property called 'b' with value 'second'
// returns 'second'

这类似于类继承,因为现在,使用新的ObjMaker()创建的任何对象都似乎继承了“b”属性。

如果你想要一个类似子类的东西,那么你可以这样做:

SubObjMaker = function () {};
SubObjMaker.prototype = new ObjMaker(); // note: this pattern is deprecated!
// Because we used 'new', the [[prototype]] property of SubObjMaker.prototype
// is now set to the object value of ObjMaker.prototype.
// The modern way to do this is with Object.create(), which was added in ECMAScript 5:
// SubObjMaker.prototype = Object.create(ObjMaker.prototype);

SubObjMaker.prototype.c = 'third';  
obj2 = new SubObjMaker();
// [[prototype]] property of obj2 is now set to SubObjMaker.prototype
// Remember that the [[prototype]] property of SubObjMaker.prototype
// is ObjMaker.prototype. So now obj2 has a prototype chain!
// obj2 ---> SubObjMaker.prototype ---> ObjMaker.prototype

obj2.c;
// returns 'third', from SubObjMaker.prototype

obj2.b;
// returns 'second', from ObjMaker.prototype

obj2.a;
// returns 'first', from SubObjMaker.prototype, because SubObjMaker.prototype 
// was created with the ObjMaker function, which assigned a for us

在最后找到这一页之前,我读了大量关于这一主题的垃圾,在那里用漂亮的图表很好地解释了这一点。

其他回答

new关键字使用函数作为构造函数创建对象的实例。例如:

var Foo = function() {};
Foo.prototype.bar = 'bar';

var foo = new Foo();
foo instanceof Foo; // true

实例继承自构造函数的原型。因此,在上面的例子中。。。

foo.bar; // 'bar'

它做5件事:

它将创建一个新对象。此对象的类型只是对象。它将此新对象的内部、不可访问的[[prototype]](即__proto__)属性设置为构造函数的外部、可访问的原型对象(每个函数对象都自动具有原型属性)。它使该变量指向新创建的对象。每当提到这一点时,它都会使用新创建的对象来执行构造函数。它返回新创建的对象,除非构造函数返回非空对象引用。在这种情况下,将返回该对象引用。

注意:构造函数是指new关键字之后的函数,如

new ConstructorFunction(arg1, arg2)

完成此操作后,如果请求了新对象的未定义属性,脚本将检查对象的[[prototype]]对象的属性。这就是如何在JavaScript中获得类似于传统类继承的东西。

这方面最困难的部分是第2点。每个对象(包括函数)都有一个名为[[prototype]]的内部属性。只能在对象创建时使用new、object.create或基于文本(函数默认为Function.prototype,数字默认为Number.prototype等)设置它。只能使用object.getPrototypeOf(someObject)读取它。没有其他方法设置或读取此值。

除了隐藏的[[prototype]]属性外,函数还有一个名为prototype的属性,您可以通过它访问和修改,为所创建的对象提供继承的财产和方法。


下面是一个示例:

ObjMaker = function() {this.a = 'first';};
// ObjMaker is just a function, there's nothing special about it that makes 
// it a constructor.

ObjMaker.prototype.b = 'second';
// like all functions, ObjMaker has an accessible prototype property that 
// we can alter. I just added a property called 'b' to it. Like 
// all objects, ObjMaker also has an inaccessible [[prototype]] property
// that we can't do anything with

obj1 = new ObjMaker();
// 3 things just happened.
// A new, empty object was created called obj1.  At first obj1 was the same
// as {}. The [[prototype]] property of obj1 was then set to the current
// object value of the ObjMaker.prototype (if ObjMaker.prototype is later
// assigned a new object value, obj1's [[prototype]] will not change, but you
// can alter the properties of ObjMaker.prototype to add to both the
// prototype and [[prototype]]). The ObjMaker function was executed, with
// obj1 in place of this... so obj1.a was set to 'first'.

obj1.a;
// returns 'first'
obj1.b;
// obj1 doesn't have a property called 'b', so JavaScript checks 
// its [[prototype]]. Its [[prototype]] is the same as ObjMaker.prototype
// ObjMaker.prototype has a property called 'b' with value 'second'
// returns 'second'

这类似于类继承,因为现在,使用新的ObjMaker()创建的任何对象都似乎继承了“b”属性。

如果你想要一个类似子类的东西,那么你可以这样做:

SubObjMaker = function () {};
SubObjMaker.prototype = new ObjMaker(); // note: this pattern is deprecated!
// Because we used 'new', the [[prototype]] property of SubObjMaker.prototype
// is now set to the object value of ObjMaker.prototype.
// The modern way to do this is with Object.create(), which was added in ECMAScript 5:
// SubObjMaker.prototype = Object.create(ObjMaker.prototype);

SubObjMaker.prototype.c = 'third';  
obj2 = new SubObjMaker();
// [[prototype]] property of obj2 is now set to SubObjMaker.prototype
// Remember that the [[prototype]] property of SubObjMaker.prototype
// is ObjMaker.prototype. So now obj2 has a prototype chain!
// obj2 ---> SubObjMaker.prototype ---> ObjMaker.prototype

obj2.c;
// returns 'third', from SubObjMaker.prototype

obj2.b;
// returns 'second', from ObjMaker.prototype

obj2.a;
// returns 'first', from SubObjMaker.prototype, because SubObjMaker.prototype 
// was created with the ObjMaker function, which assigned a for us

在最后找到这一页之前,我读了大量关于这一主题的垃圾,在那里用漂亮的图表很好地解释了这一点。

摘要:

new关键字在JavaScript中用于从构造函数创建对象。new关键字必须放置在构造函数调用之前,并将执行以下操作:

创建新对象将此对象的原型设置为构造函数的原型属性将this关键字绑定到新创建的对象并执行构造函数返回新创建的对象

例子:

功能狗(年龄){this.age=年龄;}const doggie=新狗(12);console.log(doggie);console.log(Object.getPrototypeOf(doggie)==Dog.prototype)//true

具体情况:

const doggie说:我们需要内存来声明变量。赋值运算符=表示:我们将使用=表达式是new Dog(12)。JavaScript引擎看到新关键字,创建一个新对象,并将原型设置为Dog.prototype执行构造函数时,将此值设置为新对象。在此步骤中,将年龄指定给新创建的小狗对象。新创建的对象被返回并分配给变量doggie。

好吧,JavaScript本身可能因平台而异,因为它始终是原始规范ECMAScript(ES)的实现。

在任何情况下,与实现无关,所有遵循ECMAScript规范的JavaScript实现都将为您提供一种面向对象的语言。根据ES标准:

ECMAScript是一种面向对象的编程语言,用于执行计算和操纵计算对象在主机环境中。

现在我们已经同意JavaScript是ECMAScript的一种实现,因此它是一种面向对象的语言。在任何面向对象的语言中,新操作的定义都表示,这样的关键字用于从特定类型的类(包括匿名类型,例如C#)创建对象实例。

在ECMAScript中,我们不使用类,您可以从规范中了解到:

ECMAScript不使用C++、Smalltalk或Java中的类。相反,可以通过各种方式创建对象,包括通过文字符号或通过构造函数创建对象,然后执行代码,通过指定初始值初始化所有或部分对象其财产的值。每个构造函数都是一个函数属性名为―prototype‖,用于实现基于原型的继承和共享财产。对象由创建在新表达式中使用构造函数;例如,新Date(2009,11)创建一个新的Date对象。调用构造函数不使用new的结果取决于构造函数。例如,Date()生成当前日期和时间,而不是对象。

JavaScript不是面向对象编程(OOP)语言。因此,JavaScript中的查找过程使用委托过程,也称为原型委托或原型继承。

如果您试图从一个没有的对象中获取属性的值,JavaScript引擎会查找该对象的原型(以及它的原型,一步一步)。它是原型链,直到链结束为null,即Object.prototype==null(标准对象原型)。

此时,如果未定义属性或方法,则返回undefined。

重要的函数是一类对象。

函数=函数+对象组合框

FunctionName.prototype={共享子对象}

{
  // other properties
  prototype: {
   // shared space which automatically gets [[prototype]] linkage
      when "new" keyword is used on creating instance of "Constructor
      Function"
  }
}

因此,使用新关键字。,

手动创建对象,例如newObj。使用JavaScript规范[[prototype]](即proto)中的proto(AKA:dunderproto)创建隐藏键引用并将财产分配给newObj返回newObj对象。

所有操作都是手动完成的。

function CreateObj(value1, value2) {
  const newObj = {};
  newObj.property1 = value1;
  newObj.property2 = value2;
  return newObj;
}
var obj = CreateObj(10,20);

obj.__proto__ === Object.prototype;              // true
Object.getPrototypeOf(obj) === Object.prototype // true

JavaScript关键字new有助于自动化此过程:

创建了一个新的对象文本,其标识如下:{}引用并为此分配财产隐藏键创建[[prototype]](即proto)到Function.prototype共享空间。此对象{}的隐式返回

function CreateObj(value1, value2) {
  this.property1 = value1;
  this.property2 = value2;
}

var obj = new CreateObj(10,20);
obj.__proto__ === CreateObj.prototype             // true
Object.getPrototypeOf(obj) == CreateObj.prototype // true

调用不带new关键字的构造函数:

=>此:窗口

function CreateObj(value1, value2) {
  var isWindowObj = this === window;
  console.log("Is Pointing to Window Object", isWindowObj);
  this.property1 = value1;
  this.property2 = value2;
}
var obj = new CreateObj(10,20); // Is Pointing to Window Object false
var obj = CreateObj(10,20); // Is Pointing to Window Object true
window.property1; // 10
window.property2; // 20