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);
- 不要在子类中重用父类的属性名;
避免继承标准类:继承标准类往往会由于一些特殊的内部属性而破坏;
猴子补丁:
- 避免使用轻率的猴子补丁;
- 记录程序库中所执行的所有猴子补丁;
javascript优化--02高质量编码的更多相关文章
- javascript优化--01高质量编码
javascript的浮点数: Javascript的数字都是双精度浮点数: 64位编码数字: 能表达53位精度的整数: 进行位运算时会隐式地转化为32位整数(0,1序列)后计算: 浮点数运算可能会有 ...
- javascript优化--04高质量编码
库和API的设计: 在参数设计中保持好的习惯:如顺序,width,height;top,right,bottom,left;如命名: 将undefined看作没有值而不要表示非特定的值: 在允许0,空 ...
- javascript优化--03高质量编码
使用Object的直接量实例构造轻量级的字典: 使用for/in循环,使用对象字面量来构建,并确保不增加Object.prototype属性来导致for/in循环:(考虑到兼容性,如Array.pro ...
- (第一章)改善JavaScript,编写高质量代码。
根据<编写高质量代码改善JavaScript程序的188个建议>这本书,来记录我目前所了解的建议方式. 建议1:警惕Unicode乱码 根据ECMA标准规定JavaScript语言可以使用 ...
- (第二章)改善JavaScript,编写高质量代码。
建议34:字符串是非值操作 var a = "javascript"; var b = a; b = b.toUpperCase(); alert(a); //javascript ...
- 编写高质量JavaScript代码的68个有效方法
简介: <Effective JavaScript:编写高质量JavaScript代码的68个有效方法>共分为7章,分别涵盖JavaScript的不同主题.第1章主要讲述最基本的主题,如版 ...
- 深入理解JavaScript系列(1):编写高质量JavaScript代码的基本要点
深入理解JavaScript系列(1):编写高质量JavaScript代码的基本要点 2011-12-28 23:00 by 汤姆大叔, 139489 阅读, 119 评论, 收藏, 编辑 才华横溢的 ...
- 高质量JavaScript代码书写基本要点
翻译-高质量JavaScript代码书写基本要点 by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/ ...
- [转] 翻译-高质量JavaScript代码书写基本要点 ---张鑫旭
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1173 原文作者:St ...
随机推荐
- cocos基础教程(5)数据结构介绍之cocos2d::Value
1.概述 cocos2d::Valie 是一个包含了很多原生类型(int,float,double,bool,unsigned char,char* 和 std::string)外加 std::vec ...
- js打印(控件)及多种方式
非常好用的LODOP打印控件 Lodop打印控件简单使用方法 1.安装. 2.调用LodopFuncs.js文件. 3.增加OBJECT对象 <script language="jav ...
- [Effective JavaScript 笔记]第49条:数组迭代要优先使用for循环而不是for...in循环
示例 下面代码中mean的输出值是多少? var scores=[98,74,85,77,93,100,89]; var total=0; for(var score in scores){ tota ...
- WAF绕过神器 (过安全狗、智创SQL注入)
WAF绕过神器 (过安全狗.智创SQL注入) 发布时间:-- :10文章来源:网络文章作者:panni007 点击次数: 次 分享到: QQ空间 QQ微博 新浪微博 开心网 人人网 摘要:起因: by ...
- hdparm测试硬盘性能
<1>Centos安装hdparm测试硬盘性能 一.安装hdparm yum install hdparm -y Linux学习,http:// linux.it.net.cn 二.评估读 ...
- Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- VIM中的折叠命令
参考:http://blog.csdn.net/bruce0532/article/details/8497284 za:在折叠与展开间自由转换 zf:创建折叠 示例:zf 3j #本行及以下3 ...
- iOS NSURLConnection和异步网络请求
在日常应用中,我们往往使用AFNetworking等第三方库来实现网络请求部分.这篇文章会简要地介绍一下如何使用NSURLConnection来进行异步的网络请求. 我们先看一个小demo - (vo ...
- iOS constraint被应用于view上的时间
在viewdidload时,constraint是没有被应用的,之后在layoutSubviews时,系统应用了constraint.但是我感觉在viewWillLayoutSubviews函数时就已 ...
- mysql生成varchar类型主键排序
用uuid生成20位的主键 SELECT LEFT(REPLACE(UUID(), '-', ''),20) FROM DUAL 另一种方法: 因为数据库中有字母 需要排序的时候去除字母,重新取最大值 ...