js面向对象编程,定义类的四种方法
2014-03-12 14:41
239 查看
第一种定义类的方法:
/* Anim class. */ var Anim = function() { ... }; Anim.prototype.start = function() { ... }; Anim.prototype.stop = function() { ... }; /* 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() { ... }, stop: function() { ... } };
第三种方法:
/* 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() { ... }); Anim.method('stop', function() { ... });
第四种方法:
/* 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() { ... }). method('stop', function() { ... });
相关文章推荐
- 四种常见的JS模块化管理方法的比较
- JS生成 UUID的四种方法
- js面试题:关于数组去重的四种方法总结
- 求js数组的最大值和最小值的四种方法
- 动态加载JS的四种方法
- 第164天:js方法调用的四种模式
- asp.net后台注册js的四种方法
- Javascript 将json字符串解析成js 对象的四种方法
- js面向对象编程:如何实现方法重载
- JS字符串转换为JSON的四种方法
- JS-面向对象编程-对象方法添加属性
- 求js数组的最大值和最小值的四种方法
- 求js数组的最大值和最小值的四种方法
- 判断JS数据类型的四种方法
- 求js数组的最大值和最小值的四种方法
- 四种方法js实现点击<li>弹出索引
- JS生成 UUID的四种方法
- 利用JS判断客户端类型你应该知道的四种方法
- JS中检测数据类型的四种方法
- 判断js数据类型的四种方法,以及各自的优缺点(很详细哦)