Dan*_*Dan 22 javascript arrays oop prototype class
之前有人注意到这种行为吗?这真的让我失望......我原本希望原型数组对每个类实例都是私有的,而不是在所有类实例之间共享.
有人可以验证这是正确的行为,并可能更详细地解释这种行为?
请注意注释的代码以及它如何影响脚本的行为.
<html>
<head>
<script type="text/javascript">
function print_r( title, object ) {
var output = '';
for( var key in object ) {
output += key + ": " + object[ key ] + "\n";
}
output = title + "\n\n" + output;
alert( output );
}
function Sandwich() {
// Uncomment this to fix the problem
//this.ingredients = [];
}
Sandwich.prototype = {
"ingredients" : [],
"addIngredients" : function( ingArray ) {
for( var key in ingArray ) {
this.addIngredient( ingArray[ key ] );
}
},
"addIngredient" : function( thing ) {
this.ingredients.push( thing );
}
}
var cheeseburger = new Sandwich();
cheeseburger.addIngredients( [ "burger", "cheese" ] );
var blt = new Sandwich();
blt.addIngredients( [ "bacon", "lettuce", "tomato" ] );
var spicy_chicken_sandwich = new Sandwich();
spicy_chicken_sandwich.addIngredients( [ "spicy chicken pattie", "lettuce", "tomato", "honey dijon mayo", "love" ] );
var onLoad = function() {
print_r( "Cheeseburger contains:", cheeseburger.ingredients );
};
</script>
</head>
<body onload="onLoad();">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
非常感谢.
Fel*_*ing 33
对象的原型只是一个对象.原型属性在从该对象继承的所有对象之间共享.如果您创建"类"的新实例(JS中不存在类),则不会创建属性的副本,即从原型继承的对象.
它只会对您使用这些继承属性的方式产生影响:
function Foo() {}
Foo.prototype = {
array: [],
func: function() {}
}
a = new Foo();
b = new Foo();
a.array.push('bar');
console.log(b.array); // prints ["bar"]
b.func.bar = 'baz';
console.log(a.func.bar); // prints baz
Run Code Online (Sandbox Code Playgroud)
在所有这些情况下,您始终使用相同的对象.
但是,如果为对象的属性赋值,则将在对象本身而不是其原型上设置/创建属性,因此不会共享:
console.log(a.hasOwnProperty('array')); // prints false
console.log(a.array); // prints ["bar"]
a.array = ['foo'];
console.log(a.hasOwnProperty('array')); // prints true
console.log(a.array); // prints ["foo"]
console.log(b.array); // prints ["bar"]
Run Code Online (Sandbox Code Playgroud)
如果要为每个实例创建自己的数组,则必须在构造函数中定义它:
function Foo() {
this.array = [];
}
Run Code Online (Sandbox Code Playgroud)
因为在这里,this指的new是你打电话时生成的对象new Foo().
经验法则是:应将实例特定数据分配给构造函数内的实例,应将共享数据(如方法)分配给原型.
您可能希望阅读对象模型的详细信息,该模型描述了基于类的语言与基于原型的语言之间的差异以及对象实际的工作方式.
更新:
您可以通过Object.getPrototypeOf(obj)(可能无法在非常旧的浏览器中工作)访问对象的原型,并Object.getPrototypeOf(a) === Object.getPrototypeOf(b)为您提供true.它是同一个对象,也称为Foo.prototype.