【发布时间】:2017-03-24 09:13:14
【问题描述】:
好的,这是我正在使用 atm 的一些代码。
我的问题是我并没有真正理解对象和对象原型之间的区别。到目前为止,我一直在想,如果我制作一个新对象,例如new Memory,它继承了我在 Memory 中声明的所有属性。但是,在我的代码中,我需要向 Memory.prototype 添加选项。
所以我的核心问题是:object的properties和object.prototype的properties有什么区别?
编辑:指定: 在内存功能中,我记录了 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? -
这是在代码中进一步声明的函数,不用担心
标签: javascript object prototype