好的,這裏有一些我正在使用atm的代碼。Javascript中的對象和object.prototype之間的區別
我的問題是,我真的不明白對象和對象原型之間的差異。直到現在,我還以爲,如果我創建一個新的對象,例如新的內存,這繼承了我在內存中聲明的所有屬性。但是,在我的代碼中,我需要爲Memory.prototype添加選項。
所以我的核心問題是:是什麼Object.prototype中的對象的屬性和屬性之間的區別?
編輯:在記憶功能中指定: 我登錄this.options。這不起作用,除非我包括Memory.prototype.options = {...}。如果一個新的Memory從Memory中繼承了這些屬性,並且我定義瞭如上面的this.options.availableCards,爲什麼我需要向原型添加選項?
var createMemory = function(){
new Memory({wrapperID: 'memory-game'});
};
var Memory = function (options) {
//check for required options
if ((options.wrapperID === undefined)){
console.error('ERROR: not all required options given. wrapperID is required!');
return false;
}
//hardcoded values
this.options.availableCards = 22;
this.options.cols = 4;
this.options.rows = 4;
this.options.fliptime = this.options.flipTime || 1; //set default
this.options = extend({}, this.options);
extend(this.options, options);
//this._init();
console.log(this.options);
};
// why is this required?
Memory.prototype.options = {
onGameEnd: function(){
return false;
}
};
createMemory();
什麼是'extend'? –
這是在代碼中聲明的函數,不用擔心 –