如何正确使用javascript 来进行我们的程序开发

时间:2021-05-18

今天在github 上面找到了一个关于如何正确使用javascript 来进行我们的程序开发.我就恬不知耻的来了个原创啊..坑爹啊.拿来和大家分享一下吧.
A mostly reasonable approach to Javascript.
Types //类型
Objects //对象
Arrays //数组
Strings //字符串
Functions //函数
Properties //属性
Variables //变量
Hoisting //变量提升
Conditional Expressions & Equality //条件表达式和等式.
Blocks //块代码
Comments //注释
Whitespace //空格
Commas //逗号
Semicolons //分号
Type Casting & Coercion //类型转换
Naming Conventions //命名规则
Accessors //访问
Constructors //构造器
Events //时间
Modules //模型
jQuery //
ECMAScript 5 Compatibility //ECMA 5 兼容
Testing //测试
Performance //性能
Resources //资源
In the Wild
Translation
The JavaScript Style Guide Guide
Contributors
License

Types (类型)
原始类型: 当访问一个原始类型的时候,其实直接访问该原始类型的内容.
string
number
boolean
null
undefined
var foo = 1,
bar = foo;
bar = 9;
console.log(foo,bar); //=> 1,9

复杂类型: 当你访问一个复杂类型数据类型的时候,其实是通过引用访问该变量的值.
object
array
function

var foo = [1,2];bar = foo;bar[0] = 9;console.log(foo[0],bar[0]); // => 9,9

object(对象)
使用对象字面量来创建对象 (literal)

//badvar item = new Object();//goodvar item = {};

不要使用保留关键字作为对象的属性名.这在IE8下无法工作.

//badvar superman = {default: {clark: 'kent'},private: true};//goodvar superman = {defaults: {clark: 'kent'},hidden: true};

array(数组)
同样使用 字面量方法来创建数组

//badvar items = new Array();//goodvar items = [];

如果你不知道数组的长度,那么使用Array的内置方法push进行插入操作

var someStack = [];//badsomeStack[someStack.length] = 'vein';//goodsomeStack.push('vein');

当你想要拷贝一个数组的时候,使用array.slice

var len = items.length, //指的就是上面的内容...itemCopy = [],i;//badfor(i = 0; i < len ; ++i){itemCopy[i] = items[i];}//gooditemCopy = items.slice(); //这里要注意了.这个我还真不知道...

Strings 字符串
使用单引号 (single quotes ) 来包围字符串...//这里我没有找到合适的关于性能方面的解释,我个人也喜欢这么用,(穿的少总比穿得多好看点吧..你懂得..)

//badvar name = "Bob Parr";//goodvar name = 'Bob Parr';//badvar fullName = "Bob " + this.lastName;//goodvar fullName = 'Bob ' + this.lastName;

字符串长于80个字符的时候需要使用字符串连接在多行进行编写..注意,如果过度使用,连接字符串将会影响性能(performance)

// badvar errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';// badvar errorMessage = 'This is a super long error that was thrown because \of Batman. When you stop to think about how Batman had anything to do \with this, you would get nowhere \fast.';// goodvar errorMessage = 'This is a super long error that was thrown because ' +'of Batman. When you stop to think about how Batman had anything to do ' +'with this, you would get nowhere fast.';

如果是有计划的 建立一个数组,像下面这样.使用Array.join 效果会更好..

var items,messages,length,i;messages = [{stat: 'success',message: ' This one worked'},{stat: 'success',message: ' This one worked'},{stat: 'success',message: ' This one worked'}];length = messages.length;//badfunction inbox(messages){items = '<ul>';for (i = 0; i < length; i++) {items += '<li>' + messages[i].message + '</li>';}return items + '</ul>';}//goodfunction inbox(messages){items = [];for( i = 0; i < length ; i++){items[i] = messages[i].message;}return '<ul><li>' + items.join('</li><li>') + '</li></ul>';}

函数(Functions)

//匿名函数表达式..var anonymous = function(){return true;};// 命名函数表达式.var named = function named(){return true;};//即时引用函数(function(){console.log('Welcome to the Internet. Please follow me.');})();

永远不要在非函数的块代码(if,while)中定义函数.相应的,在代码块中间函数赋值给外部的变量名..

//badif(currentUser){function test(){console.log('Nope.');}}//goodvar test;if(currentUser){test = function(){console.log('Yup'); }; //be careful with the semi-colon.}

Properties (属性)
使用点语法来访问属性.

var luke = {jedi: true,age: 28};//badvar isJedi = luke['jedi'];//goodvar isJedi = luck.jedi;

当使用变量访问对象属性时,使用 [] 方括号来访问

var luke = {jedi: true,age: 28};function getProp(prop) {return luke[prop];}var isJedi = getProp('jedi');

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章