您好,欢迎来到华佗健康网。
搜索
您的当前位置:首页newObject()、Object.create()、{}三种对象创建方式的区别

newObject()、Object.create()、{}三种对象创建方式的区别

来源:华佗健康网
newObject()、Object.create()、{}三种对象创建⽅式的区

new Object():

  new运算符创建⼀个⽤户定义的对象类型的实例或者具有构造函数的内置对象的实例。new关键字会进⾏:    1、创建⼀个空的JavaScript对象({})

    2、链接该对象(设置该对象的构造函数)到另⼀个对象    3、将1中新创建的对象作为this的上下⽂    4、如果该函数没有返回对象,就会返回this

当你执⾏

var o = new Foo();

实际上执⾏了

var o = new Object();

o.__proto__ = Foo.prototype;Foo.call(o);

  desc:

    1、通过编写函数来定义对象类型    2、通过new来创建对象实例

  创建⼀个对象类型,需要创建⼀个指定其名称和属性的函数;对象的属性可以指向其它对象

  当代码 new Foo(...) 执⾏的时候

    1、⼀个继承⾃Foo.prototype的新对象被创建

    2、使⽤指定的参数调⽤构造函数Foo,并将this绑定到新创建的对象。new Foo等同于Foo(),也就是没有指定参数列表,Foo不带任何参数调⽤的情况

    3、由构造函数返回的对象就是new表达式的结果。如果构造函数没有显式返回⼀个对象,就是⽤步骤⼀创建的对象。(⼀般情况下,构造函数不返回值,但是⽤户可以选择主动返回对象,来覆盖正常的对象创建步骤)

  如果没有使⽤new运算符,构造函数会像其它的常规函数⼀样被调⽤,并不会创建⼀个对象。这种情况下,this的指向也是不⼀样的。

Object.create():

  创建⼀个新对象,使⽤现有的对象来提供新创建的对象的__proto__

const person = { isHuman: false,

printIntroduction: function () {

console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`); }};

const me = Object.create(person);

me.name = \"Matthew\"; // \"name\" 这个属性只被写在me上, 没有被写在\"person\"上me.isHuman = true; // 继承的属性可被重写

me.printIntroduction();

// expected output: \"My name is Matthew. Am I human? true\"

  语法:Object.create(proto, [propertiesObject])    proto:新创建对象的原型对象

    propertiesObject:可选的,如果没有指定为undefined,要添加到新创建对象的可枚举属性(即⾃⾝定义的属性,⽽不是其原型链上的枚举属性)对象的属性描述符以及相应的属性名称。这些属性相对应Object.definedProperties()的第⼆个参数  返回值:⼀个新对象带着指定的原型对象和属性

  例外:如果propertiesObject参数是null或者是⾮原始包装对象,就会抛出⼀个TypeError异常。

// Shape - ⽗类(superclass)function Shape() { this.x = 0; this.y = 0;}

// ⽗类的⽅法

Shape.prototype.move = function(x, y) { this.x += x; this.y += y;

console.info('Shape moved.');};

// Rectangle - ⼦类(subclass)function Rectangle() {

Shape.call(this); // call super constructor.}

// ⼦类续承⽗类

Rectangle.prototype = Object.create(Shape.prototype);Rectangle.prototype.constructor = Rectangle;var rect = new Rectangle();

console.log('Is rect an instance of Rectangle?',rect instanceof Rectangle); // trueconsole.log('Is rect an instance of Shape?',rect instanceof Shape); // truerect.move(1, 1); // Outputs, 'Shape moved.'

  如果要是希望能够继承多个对象,就可以使⽤混⼊的⽅式:

function MyClass() {

SuperClass.call(this);

OtherSuperClass.call(this);}

// 继承⼀个类

MyClass.prototype = Object.create(SuperClass.prototype);// 混合其它

Object.assign(MyClass.prototype, OtherSuperClass.prototype);// 重新指定constructor

MyClass.prototype.constructor = MyClass;MyClass.prototype.myMethod = function() { // do a thing};

  使⽤Object.create的propertiesObject参数

var o;

// 创建⼀个原型为null的空对象o = Object.create(null);

o = {};

// 以字⾯量⽅式创建的空对象就相当于:o = Object.create(Object.prototype);

o = Object.create(Object.prototype, { // foo会成为所创建对象的数据属性 foo: {

writable:true,

configurable:true, value: \"hello\" },

// bar会成为所创建对象的访问器属性 bar: {

configurable: false,

get: function() { return 10 }, set: function(value) {

console.log(\"Setting `o.bar` to\ } }});

function Constructor(){}o = new Constructor();// 上⾯的⼀句就相当于:

o = Object.create(Constructor.prototype);

// 当然,如果在Constructor函数中有⼀些初始化代码,Object.create不能执⾏那些代码

// 创建⼀个以另⼀个空对象为原型,且拥有⼀个属性p的对象o = Object.create({}, { p: { value: 42 } })

// 省略了的属性特性默认为false,所以属性p是不可写,不可枚举,不可配置的:o.p = 24o.p//42

o.q = 12

for (var prop in o) { console.log(prop)}//\"q\"delete o.p//false

//创建⼀个可写的,可枚举的,可配置的属性po2 = Object.create({}, { p: {

value: 42, writable: true, enumerable: true, configurable: true } });

{}:

  是JavaScript对象字⾯量创建的形式,其本质和new Object() 并⽆区别,默认都是继承了Object对象上的prototype

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.com 版权所有 湘ICP备2023021991号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务