栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JavaScript中的“ new”关键字是什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaScript中的“ new”关键字是什么?

它做五件事:

  1. 它创建一个新对象。这个对象的类型就是 object
  2. 它将这个新对象的内部不可访问的 [[prototype]] (即 proto )属性设置为构造函数的外部可访问 原型 对象(每个函数对象都会自动具有 prototype 属性)。
  3. 它使
    this
    变量指向新创建的对象。
  4. 每当
    this
    提及时,它都会使用新创建的对象执行构造函数。
  5. 除非构造函数返回非
    null
    对象引用,否则它将返回新创建的对象。在这种情况下,将返回该对象引用。

注意: 构造函数 是指

new
关键字后面的函数,如

new ConstructorFunction(arg1, arg2)

完成此操作后,如果请求新对象的未定义属性,则脚本将改为检查对象的 [[prototype]]
对象的属性。这就是您可以获得类似于Javascript中传统类继承的方式的方法。

关于这一点,最困难的部分是点号2。每个对象(包括函数)都具有称为 [[prototype]]的 内部属性。它可以
被在创建对象的时间与设置,无论是 新的 ,具有 的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 withobj1 = 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'

就像类继承一样,因为现在,您使用的任何对象

new 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.prototypeobj2.c;// returns 'third', from SubObjMaker.prototypeobj2.b;// returns 'second', from ObjMaker.prototypeobj2.a;// returns 'first', from SubObjMaker.prototype, because SubObjMaker.prototype // was created with the ObjMaker function, which assigned a for us


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/376577.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号