美文网首页
Javascript创建对象的5种姿势

Javascript创建对象的5种姿势

作者: 小小人喃 | 来源:发表于2017-11-15 11:55 被阅读0次
/* 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');
  });

相关文章

  • JS笔记-006-JS对象-数字-字符串-日期-数组-逻辑

    JS对象 创建 JavaScript 对象 通过 JavaScript,您能够定义并创建自己的对象。 创建新对象有...

  • Javascript创建对象的5种姿势

  • JavaScript基础之创建对象

    JavaScript对象的创建 在JavaScript中创建一个对象有三种方式。可以通过对象直接量、关键字new和...

  • 08-JavaScript面向对象

    创建对象 JavaScript中如何创建对象 通过默认的Object这个类(构造函数)来创建 通过字面量来创建对象...

  • Javascript全局属性和方法

    JavaScript 全局属性和方法 JavaScript 可用于创建Javascript对象。 CONTENT ...

  • JavaScript创建对象

    创建对象 1.工厂模式 2.构造函数模式 3.原型模式 4.组合使用构造函数模式和原型模式 5.动态原型模式 6....

  • JavaScript — 创建对象

    一、工厂模式 可以无数次调用上面这个函数,解决创建多个相似对象的问题,但是没有解决对象识别的问题(即怎样知道一个对...

  • JavaScript 创建对象

    我们知道可以用Object的构造函数或对象字面量来创建对象,但是采用这些方式,创建多个对象,会产生大量重复的代码,...

  • JavaScript创建对象

    JavaScript中创建对象有以下七种方式: 工厂模式构造函数模式原型模式构造函数和原型组合模式动态原型模式寄生...

  • 【javascript】创建对象

    虽然Object 构造函数或对象字面量都可以用来创建单个对象,但这些方式有个明显的缺点:使用同一个接口创建很多对象...

网友评论

      本文标题:Javascript创建对象的5种姿势

      本文链接:https://www.haomeiwen.com/subject/hyucvxtx.html