JavaScript摘要
2016-01-16 13:43
399 查看
1 采用匿名自执行函数定义对象
var Size = (function size() {
function constructor(w, h) {
this.w = w;
this.h = h;
}
constructor.prototype = {
toString: function () {
return "(" + this.w + ", " + this.h + ")";
},
getHalfSize: function () {
return new Size(this.w >>> 1, this.h >>> 1);
},
length: function () {
return this.w * this.h;
}
};
return constructor;
})();
此写法表示在该代码解释时就自动会执行(function size())中的方法
目的,避免污染外部的变量
2 get set访问器
var Size = (function size() {
function constructor(w, h) {
this.w = w;
this.h = h;
}
constructor.prototype = {
toString: function () {
return "(" + this.w + ", " + this.h + ")";
},
getHalfSize: function () {
return new Size(this.w >>> 1, this.h >>> 1);
},
length: function () {
return this.w * this.h;
}
};
return constructor;
})();
此写法表示在该代码解释时就自动会执行(function size())中的方法
目的,避免污染外部的变量
2 get set访问器
var log = ['test']; var obj = { get latest () { if (log.length == 0) return undefined; return log[log.length - 1] } } console.log (obj.latest); // Will return "test".
相关文章推荐
- JavaScript面向对象精要
- javascript学习笔记(一)
- JavaScript 学习摘要(2)
- AnjularJs的应用
- JavaScript中的方法重载
- js 获取地址栏参数
- 读取本地Json文件
- 本地 json文件转化成plist文件
- JS-JavaScript学习笔记(一)
- 解决Eclipse报errors running builder ‘javascript validator’ on project
- classList详解,让你的js方便地操作DOM类
- JSON格式解析和libjson使用简介-转
- cJSON库介绍
- 三款简单的JS返回页面顶部代码
- js-事件
- eclipse中默认HTML和js编辑器非常慢,尤其在拷贝粘贴代码时的解决方法
- js study
- JavaScript基本用法
- js文章内容
- JS之动态表格