//declaring the constructor
function ArrayMaker(arg1, arg2) {
this.someProperty = 'whatever';
this.theArray = [ this, arg1, arg2 ];
}
// declaring instance methods
ArrayMaker.prototype = {
someMethod: function () {
alert( 'someMethod called');
},
getArray: function () {
return this.theArray;
}
};
var am = new ArrayMaker( 'one', 'two' );
var other = new ArrayMaker( 'first', 'second' );
am.getArray();
// => [ am, 'one' , 'two' ]
Javascript Constructor
最新推荐文章于 2025-07-02 10:02:23 发布
本文介绍了一个使用构造函数创建对象实例的方法,并展示了如何通过原型对象为这些实例添加共享方法。具体实现了ArrayMaker构造函数,该构造函数接受两个参数并将它们与实例本身一起存储在一个数组中。此外,还定义了两个原型方法:someMethod用于显示消息,getArray则返回存储的数组。
932

被折叠的 条评论
为什么被折叠?



