Javascript创建对象的5种姿势
2017-11-17 10:04
197 查看
/* 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创建对象的5种模式
- 从面试题学习Javascript——面向对象(创建对象)
- 深入理解JavaScript中创建对象模式的演变(原型)
- Javascript中创建对象的几种类型总结
- javaScripte创建对象窗口
- JavaScript创建对象的七种方式
- JavaScript 三种创建对象的方法
- JavaScript中对象创建的几种方式
- 深入理解JavaScript系列(47):对象创建模式(上篇)
- JavaScript创建对象的写法
- JavaScript创建对象的七种方式全面总结
- javascript中创建有方法的自定义对象
- javascript创建createXmlHttpRequest对象示例代码
- javascript对象的创建
- Javascript 创建对象的三种方法及比较【转载+整理】
- JavaScript创建对象的几种方式
- JavaScript 获取标签对象,创建标签对象
- javascript 技术创建对象
- javascript创建自定义对象和prototype