码迷,mamicode.com
首页 > 编程语言 > 详细

【笔记】JavaScript编码规范- 存取器

时间:2015-05-20 09:45:38      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:javascript

对于属性,访问器函数不是必须的。

如果定义了存取器函数,应参照getVal() 和 setVal(‘Hi’)格式。

// bad
dragon.age();


// good
dragon.getAge();


// bad
dragon.age(25);


// good
dragon.setAge(25);

如果属性时boolean,格式应为isVal() or hasVal().
// bad
if (!dragon.age()) {
return false;
}


// good
if (!dragon.hasAge()) {
return false;
}
创建get() and set()函数时不错的想法,但是要保持一致

function Test(options) {
options || (options = {});
var lightsaber = options.lightsaber || 'blue';
this.set('lightsaber', lightsaber);
}


Test.prototype.set = function(key, val) {
this[key] = val;
};


Test.prototype.get = function(key) {
return this[key];
};


Genesis 1:16 And God made two great lights;the greater light to rule the day,and the lesser light to rule the night:he made the stars alse.

【笔记】JavaScript编码规范- 存取器

标签:javascript

原文地址:http://blog.csdn.net/princeterence/article/details/45866081

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!