javascript优化--02高质量编码

方法调用:

  • 通常某个对象调用方法查找该方法并将该对象作为该方法的接受者(this);
  • 使用call自定义接受者
    • 可以调用在给定对象中不存在的方法;
    • 定义高阶函数,允许使用者给回调函数指定接受者;

使用bind方法:

  • 当高阶函数传递对象方法时,可以使用匿名函数在适当的接受者上调用方法,或使用bind绑定接受者;
  • 使用bind方法实现函数的柯里化(创建一个固定函数子集的委托函数),这里一般绑定接受者null;

使用高阶函数:

  • 高阶函数就是将函数作为参数或返回值的函数;
  • 出现重复或相似的代码时,可以考虑高阶函数;

arguments对象:

  • 永远不要修改arguements对象;
  • 使用[].slice.call(arguments)复制到真正数组之后再操作;
  • 在嵌套函数中最好将arguments绑定到一个新变量中;

arguments.caller/arguments.callee/caller;

  • 避免使用非标准的arguments.caller和arguments.callee属性,因为不具备良好的移植性;
  • 避免使用函数对象的caller属性,因为在包含全部栈信息方面,它是不可靠的;

函数对象的toString方法:

  • 避免使用函数对象的toString方法;
  • 不同情引擎下调用toString方法的结果可能不同,在使用它提取函数源代码时不值得信赖;

new操作构造函数:

在使用构造函数时可能忘记new,一般可以将构造函数写为:

function User(name, password) {
   if(!(this instanceof User)) {
      return new User(name, password);
   }
  this.name = name;
  this.password = password;
}

但它需要额外函数调用,可以选择更优方法:

function User(name, password) {
   var self = this.instanceof User
               ?  this
               :  Object.create(User.prototype);
   self.name = name;
   self.password = password;
   return self;
}

对于没有Object.create方法,可以简单实现其单参数方法

Object.create = function(prototype) {
   function C() {};
   C.prototype = prototype;
   return new C(); 
}

私有数据:

  • 在构造函数中保存私有属性,更倾向于使用闭包,而不是用下划线标记属性的形式;
  • 相对的,这些方法必须置于实例对象中;  

构建子类/父类继承:

function Scene(context, width, height, images) {
   this.context = context;
     this.width = width;
     this.height = height;
     this.images = images;
     this.actors = [];
}
Scene.prototype.register = function(actor) {
  this.actors.push(actor);
}  
Scene.prototype.unregister = function(actor) {
 var i = this.actors.indexOf(actor);
    if(i >= 0) {
      this.splice(i,1);
    }
}
Scene.prototype.draw = function() {
 this.context.clearRect(0, 0, this.width, this.height);
 for(var a = this.actors, i = 0, n = a.length;
     i < n;
     i++) {
    a[i].draw();
 }
}
 
//---------------------------------
function Actor(scene, x, y) {
    this.scene = scene;
    this.x = x;
    this.y = y;
    scene.register(this);
}
Actor.prototype.moveTo = function(x, y) {
    this.x = x;
    this.y = y;
    this.scene.draw();
}
Actor.prototype.exit = function () {
    this.scene.unregister(this);
    this.scene.draw();
}
Actor.prototype.draw = function() {
    varimage = this.scene.images[this.type];
    this.scene.context.drawImage(image, this,x, this,y);
}
Actor.prototype.width = function() {
    return this.scene.images[this.type].width;
}
Actor.prototype.height = function() {
    return this.scene.images[this.type].height;
}
 
 
//---------------------------------
function SpaceShip(scene, x, y) {
    Actor.call(this, scene, x, y);
    this.points = 0;
}
SpaceShip.prototype = Object.create(Actor.prototype);
 
SpaceShip.prototype.type = 'spaceShip';
 
SpaceShip.prototype.scorePoint = function() {
    this.points++;
}
SpaceShip.prototype.left = function() {
    this.moveTo(Math.max(this.x - 10, 0), this.y);
}
SpaceShip.prototype.right = function() {
    var maxWidth = this.scene.width - this.width();
    this.moveTo(Math.min(this.x + 10, maxWidth), this.y);
}

  

  • 在子类构造函数中使用call显式地传入this作为接受者调用父类构造函数;
function SpaceShip(scene, x, y) {
	Actor.call(this, scene, x, y);
	this.points = 0;
}
  • 使用Object.create函数来构造子类的原型对象以避免调用父类的构造函数;
    SpaceShip.prototype = Object.create(Actor.prototype);
  • 不要在子类中重用父类的属性名;

避免继承标准类:继承标准类往往会由于一些特殊的内部属性而破坏;

猴子补丁:

  • 避免使用轻率的猴子补丁;
  • 记录程序库中所执行的所有猴子补丁;

 

原文地址:https://www.cnblogs.com/jinkspeng/p/4146265.html