我应该使用对象文字或构造函数吗?

我很困惑,我应该在JavaScript中创build一个对象的方式。 看来至less有两种方法。 一个是使用对象字面表示法,而另一个使用构造函数。 彼此之间有优势吗?

如果你没有与对象相关的行为(即,如果对象只是数据/状态的容器),我会使用对象字面值。

var data = { foo: 42, bar: 43 }; 

应用KISS原则 。 如果除了简单的数据容器之外不需要任何东西,只需简单的文字即可。

如果你想添加行为到你的对象,你可以去一个构造函数,并在构造过程中添加方法给对象,或给你的类的原型。

 function MyData(foo, bar) { this.foo = foo; this.bar = bar; this.verify = function () { return this.foo === this.bar; }; } // or: MyData.prototype.verify = function () { return this.foo === this.bar; }; 

像这样的类也可以像数据对象的模式一样工作:现在,您可以通过构造函数获得某种合约(对象初始化/包含的属性)。 一个自由文本只是一个数据的无定形数据块。

你可能有一个外部verifyfunction,作用于一个普通的旧数据对象:

 var data = { foo: 42, bar: 43 }; function verify(data) { return data.foo === data.bar; } 

然而,这在封装方面并不理想:理想情况下,与实体相关的所有数据+行为应该共存。

它基本上归结为如果你需要多个对象的实例, 用构造函数定义的对象可以让你有多个对象的实例。 对象文字基本上是单variables,方法都是公开的。

 // define the objects: var objLit = { x: 0, y: 0, z: 0, add: function () { return this.x + this.y + this.z; } }; var ObjCon = function(_x, _y, _z) { var x = _x; // private var y = _y; // private this.z = _z; // public this.add = function () { return x + y + this.z; // note x, y doesn't need this. }; }; // use the objects: objLit.x = 3; objLit.y = 2; objLit.z = 1; console.log(objLit.add()); var objConIntance = new ObjCon(5,4,3); // instantiate an objCon console.log(objConIntance.add()); console.log((new ObjCon(7,8,9)).add()); // another instance of objCon console.log(objConIntance.add()); // same result, not affected by previous line 

以统一的方式创build对象的另一种方法是使用返回对象的函数:

 function makeObject() { var that = { thisIsPublic: "a public variable" thisIsAlsoPublic: function () { alert(that.thisIsPublic); } }; var secret = "this is a private variable" function secretFunction() { // private method secret += "!"; // can manipulate private variables that.thisIsPublic = "foo"; } that.publicMethod = function () { secret += "?"; // this method can also mess with private variables } that.anotherPublicVariable = "baz"; return that; // this is the object we've constructed } makeObject.static = "This can be used to add a static varaible/method"; var bar = makeObject(); bar.publicMethod(); // ok alert(bar.thisIsPublic); // ok bar.secretFunction(); // error! bar.secret // error! 

由于JavaScript中的函数是闭包,我们可以使用私有variables和方法,并避免new

从JavaScript中的私有variableshttp://javascript.crockford.com/private.html 。

这取决于你想要做什么。 如果你想在你的对象中使用(半)私有variables或函数,构造函数是实现它的方法。 如果你的对象只包含属性和方法,那么对象文字就没有问题。

 function SomeConstructor(){ var x = 5; this.multiply5 = function(i){ return x*i; } } var myObj = new SomeConstructor; var SomeLiteral = { multiply5: function(i){ return i*5; } } 

现在这个方法在myObjSomeLiteral multiply5 SomeLiteral完全一样。 唯一的区别是myObj使用一个私有variables。 后者在某些情况下可能是有用的。 大部分时间Object对象是足够的,并且是创build一个JS对象的一个​​不错的,干净的方法。

下面的代码显示了三种创build对象的方法:Object Literal语法,Function Constructor和Object.create() 。 对象文字语法只是简单地创build和对象,因此它的__prototype__Object对象,它将有权访问Object所有属性和方法。 严格来说,从devise模式的angular度来看,应该使用简单的对象文本来存储单个数据实例。

函数构造函数有一个名为.prototype的特殊属性。 这个属性将成为由函数构造函数创build的任何对象的__prototype__ 。 添加到函数构造函数的.prototype属性中的所有属性和方法都将可用于其创build的所有对象。 如果需要多个数据实例或需要对象的行为,则应使用构造函数。 注意,当你想要模拟私有/公共开发模式时,函数构造函数也是最好的。 请记住将所有共享的方法放在.prototype上,这样它们就不会在每个对象实例中被创build。

使用Object.create()创build对象,使用对象字面值作为此方法创build的对象的__prototype__ 。 所有添加到对象字面值的属性和方法都可以通过真正的原型inheritance为从它创build的所有对象提供。 这是我的首选方法。

 //Object Example //Simple Object Literal var mySimpleObj = { prop1 : "value", prop2 : "value" } // Function Constructor function PersonObjConstr() { var privateProp = "this is private"; this.firstname = "John"; this.lastname = "Doe"; } PersonObjConstr.prototype.greetFullName = function() { return "PersonObjConstr says: Hello " + this.firstname + " " + this.lastname; }; // Object Literal var personObjLit = { firstname : "John", lastname: "Doe", greetFullName : function() { return "personObjLit says: Hello " + this.firstname + ", " + this.lastname; } } var newVar = mySimpleObj.prop1; var newName = new PersonObjConstr(); var newName2 = Object.create(personObjLit); 

使用对象字面值,随着初始值的引入,它会更加简洁和更好地扩展。

//对象文字和对象的构造函数

 function MyData(foo, bar) { this.foo = foo; this.bar = bar; } MyData.prototype.verify = function () { return this.foo === this.bar; }; //add property using prototype var MD = new MyData;//true. var MD = new MyData();//true. MD.verify// return only the function structure. MD.verify(); //return the verify value and in this case return true coz both value is null. var MD1 = new MyData(1,2); // intialized the value at the starting. MD1.verify// return only the function structure. MD1.verify(); // return false coz both value are not same. MD1.verify(3,3);// return false coz this will not check this value intialized at the top MyData.prototype.verify = function (foo,bar) { return this.foo === this.bar; }; var MD1 = new MyData(1,2); MD1.verify(); MD1.verify(3,3);// return false coz this keyword used with foo and bar that will check parent data 

在这里输入图像说明

你想单页的对象的单一实例 – 文字。

你想只传输数据像DTO对象简单GET SET: – 文字

你想用方法行为创build真实对象,多个实例 – 构造函数,遵循OOP原则,inheritance: – 构造函数。

下面是YouTube的video,详细解释什么是文字,什么是构造函数,以及它们如何彼此不同。

https://www.youtube.com/watch?v=dVoAq2D3n44

正如https://www.w3schools.com/js/js_object_definition.asp所述;

使用对象字面量,您可以在一个语句中定义创build 一个对象。

对象文字只能创build一个对象。 有时我们喜欢有一个对象types可以用来创build一个types的许多对象。