/* Start and stop animations using functions. */
function startAnimation() {
alert('startAnimation');
}
function stopAnimation() {
alert('stopAnimation');
}
/* Anim class. */
var Anim = function() {
};
Anim.prototype.start = function() {
alert('dasdasd');
};
Anim.prototype.stop = function() {
alert('dsadsasdfdsfds');
};
/* Usage. */
var myAnim = new Anim();
myAnim.start();
myAnim.stop();
/* Anim class, with a slightly different syntax for declaring methods. */
var Anim = function() {
};
Anim.prototype = {
start: function() {
alert('i am start');
},
stop: function() {
alert('i am stop');
}
};
var myAnim = new Anim();
myAnim.start();
myAnim.stop();
/* Add a method to the Function class that can be used to declare methods. */
Function.prototype.method = function(name, fn) {
this.prototype[name] = fn;
};
/* Anim class, with methods created using a convenience method. */
var Anim = function() {
};
Anim.method('start', function() {
alert('i am a start two');
});
Anim.method('stop', function() {
alert(' i am a stop two');
});
var myAnim = new Anim();
myAnim.start();
myAnim.stop();
/* This version allows the calls to be chained. */
Function.prototype.method = function(name, fn) {
this.prototype[name] = fn;
return this;
};
/* Anim class, with methods created using a convenience method and chaining. */
var Anim = function() {
};
Anim.
method('start', function() {
alert('endstart');
}).
method('stop', function() {
alert('endstop');
});
Javascript创建对象的5种姿势
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- JavaScript与传统静态语言比较几点奇葩的地方 变量不用var声明就能使用,如果变量未赋值默认为undefi...