首页 > 其他 > 详细

华为2014机试样题及代码(三)

时间:2014-03-13 06:13:14      阅读:486      评论:0      收藏:0      [点我收藏+]

 


  第一种定义类的方法:

/* 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() {
    ...
  });


华为2014机试样题及代码(三),布布扣,bubuko.com

华为2014机试样题及代码(三)

原文:http://blog.csdn.net/it_bloggers/article/details/21089497

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!