Javascript 1.9.3 / ECMAScript 5引入了Object。这是Douglas Crockford等人长期以来一直倡导的。我如何在下面的代码替换新的对象。创建?
var UserA = function(nameParam) {
this.id = MY_GLOBAL.nextId();
this.name = nameParam;
}
UserA.prototype.sayHello = function() {
console.log('Hello '+ this.name);
}
var bob = new UserA('bob');
bob.sayHello();
(假设MY_GLOBAL。nextId存在)。
我能想到的最好的是:
var userB = {
init: function(nameParam) {
this.id = MY_GLOBAL.nextId();
this.name = nameParam;
},
sayHello: function() {
console.log('Hello '+ this.name);
}
};
var bob = Object.create(userB);
bob.init('Bob');
bob.sayHello();
似乎没有任何优势,所以我想我没有得到它。我可能太新古典主义了。我应该如何使用Object。创建创建用户“bob”?
新的操作符
用于从构造函数创建对象
新的关键字还执行构造函数
function Car() {
console.log(this) // this points to myCar
this.name = "Honda";
}
var myCar = new Car()
console.log(myCar) // Car {name: "Honda", constructor: Object}
console.log(myCar.name) // Honda
console.log(myCar instanceof Car) // true
console.log(myCar.constructor) // function Car() {}
console.log(myCar.constructor === Car) // true
console.log(typeof myCar) // object
Object.create
你也可以使用Object。Create创建一个新对象
但是,它不执行构造函数
对象。Create用于从另一个对象创建一个对象
const Car = {
name: "Honda"
}
var myCar = Object.create(Car)
console.log(myCar) // Object {}
console.log(myCar.name) // Honda
console.log(myCar instanceof Car) // ERROR
console.log(myCar.constructor) // Anonymous function object
console.log(myCar.constructor === Car) // false
console.log(typeof myCar) // object
简介:
object. create()是一个Javascript函数,它接受2个参数并返回一个新对象。
第一个参数是一个对象,它将是新创建对象的原型
第二个参数是一个对象,它将是新创建对象的属性
例子:
Const proto = {
Talk: () => console.log('hi')
}
Const props = {
年龄:{
可写:没错,
可配置:没错,
价值:26
}
}
let Person = Object。创建(原型、道具)
console.log (Person.age);
Person.talk ();
实际应用:
The main advantage of creating an object in this manner is that the prototype can be explicitly defined. When using an object literal, or the new keyword you have no control over this (however, you can overwrite them of course).
If we want to have a prototype The new keyword invokes a constructor function. With Object.create() there is no need for invoking or even declaring a constructor function.
It can Basically be a helpful tool when you want create objects in a very dynamic manner. We can make an object factory function which creates objects with different prototypes depending on the arguments received.
由于只有一个继承级别,您的示例可能不会让您看到Object.create的真正好处。
该方法允许您轻松实现差异继承,其中对象可以直接从其他对象继承。
在你的userB的例子中,我不认为你的init方法应该是公共的,甚至不存在,如果你在一个现有的对象实例上再次调用这个方法,id和name属性将会改变。
对象。Create允许你使用它的第二个参数初始化对象属性,例如:
var userB = {
sayHello: function() {
console.log('Hello '+ this.name);
}
};
var bob = Object.create(userB, {
'id' : {
value: MY_GLOBAL.nextId(),
enumerable:true // writable:false, configurable(deletable):false by default
},
'name': {
value: 'Bob',
enumerable: true
}
});
如您所见,属性可以在Object的第二个参数上初始化。使用类似于object . defineproperties和object . defineproperty方法使用的语法创建一个对象文字。
它允许您设置属性属性(可枚举、可写或可配置),这非常有用。
与new object相比,使用object. create(…)并没有什么优势。
那些提倡这种方法的人通常会说一些模棱两可的优点:“可伸缩性”,或者“对JavaScript来说更自然”等等。
然而,我还没有看到一个具体的例子来显示这个对象。Create比new有任何优势。相反,它存在一些已知的问题。Sam Elsamman描述了当有嵌套对象并且使用Object.create(…)时会发生什么:
var Animal = {
traits: {},
}
var lion = Object.create(Animal);
lion.traits.legs = 4;
var bird = Object.create(Animal);
bird.traits.legs = 2;
alert(lion.traits.legs) // shows 2!!!
这是因为Object.create(…)提倡使用数据创建新对象的实践;在这里,动物数据成为狮子和鸟原型的一部分,并在共享时引起问题。当使用new时,原型继承是显式的:
function Animal() {
this.traits = {};
}
function Lion() { }
Lion.prototype = new Animal();
function Bird() { }
Bird.prototype = new Animal();
var lion = new Lion();
lion.traits.legs = 4;
var bird = new Bird();
bird.traits.legs = 2;
alert(lion.traits.legs) // now shows 4
至于传递给Object.create(…)的可选属性属性,可以使用Object.defineProperties(…)添加。