JavaScript, We Hardly new Ya
2013-12-24 14:37
302 查看
JavaScript is a prototypal language, but it has a
leading to some problematic programming patterns.
You never need to use
Do not use
Do not use
is better written as
The second form allows the compiler to see the function body sooner, so any errors in it will be detected sooner. Sometimes
If we keep function bodies in strings, the compiler can’t see them. If we keep function bodies as string expressions, we can’t see them either. It is better to not program in ignorance. By making a function that returns a function, we can explicitly pass
in the values we want to bind. This allows us to initialize a set of
It is never a good idea to put
It is better to use an object literal. It is smaller, faster.
If we are making an object containing methods that are bound to private variables and functions, it is still better to leave off the
By using
So the rule is simple: The only time we should use the
the use of
There is a time to
newoperator that tries to make it look sort of like a classical language. That tends to confuse programmers,
leading to some problematic programming patterns.
You never need to use
new Object()in JavaScript. Use the object literal
{}instead. Similarly, don’t use
new Array(), use the array literal
[]instead. Arrays in JavaScript work nothing like the arrays in Java, and use of the Java-like syntax will confuse you.
Do not use
new Number,
new String, or
new Boolean. These forms produce unnecessary object wrappers. Just use simple literals instead.
Do not use
new Functionto create function values. Use function expressions instead. For example,
frames[0].onfocus = new Function("document.bgColor='antiquewhite'")
is better written as
frames[0].onfocus = function () {document.bgColor = 'antiquewhite';};
The second form allows the compiler to see the function body sooner, so any errors in it will be detected sooner. Sometimes
new Functionis used by people who do not understand how inner functions work.
selObj.onchange = new Function("dynamicOptionListObjects["+ dol.index+"].change(this)");
If we keep function bodies in strings, the compiler can’t see them. If we keep function bodies as string expressions, we can’t see them either. It is better to not program in ignorance. By making a function that returns a function, we can explicitly pass
in the values we want to bind. This allows us to initialize a set of
selObjin a loop.
selObj.onchange = function (i) { return function () { dynamicOptionListObjects[i].change(this); }; }(dol.index);
It is never a good idea to put
newdirectly in front of
function. For example,
new functionprovides no advantage in constructing new objects.
myObj = new function () { this.type = 'core'; };
It is better to use an object literal. It is smaller, faster.
myObj = { type: 'core' };
If we are making an object containing methods that are bound to private variables and functions, it is still better to leave off the
newprefix.
var foo = new function() { function processMessages(message) { alert("Message: " + message.content); } this.init = function() { subscribe("/mytopic", this, processMessages); } }
By using
newto invoke the function, the object holds onto a worthless
prototypeobject. That wastes memory with no offsetting advantage. If we do not use the
new, we don’t keep the wasted
prototypeobject in the chain. So instead we will invoke the factory function the right way, using
().
var foo = function () { function processMessages(message) { alert("Message: " + message.content); } return { init: function () { subscribe("/mytopic", this, processMessages); } }; }();
So the rule is simple: The only time we should use the
newoperator is to invoke a pseudoclassical Constructor function. When calling a Constructor function,
the use of
newis mandatory.
There is a time to
new, and a time to not.
相关文章推荐
- JavaScript, We Hardly new Ya
- JavaScript基础 使用new关键字和Array()来创建空数组 并 不按顺序来添加元素
- JavaScript new Date()IE浏览器下出错 NaN
- javaScript系列:js中获取时间new Date()详细介绍
- javascript new fun的执行过程
- javascript技术难点(三)之this、new、apply和call详解
- javascript中new Date()的浏览器兼容性问题IE8Nan
- JavaScript-Prototype-new-继承or委托?
- JavaScript简洁继承机制实现(不使用prototype和new)
- javascript 敏感词过滤题 Cross the greatwall,We can reach every corner of the world.
- Javascript中创建字符串的new语法和literal语法
- 从mixin到new和prototype:Javascript原型机制详解
- JavaScript中的new的使用方法与注意事项
- Javascript 模拟 new
- javascript new Object(value)
- javascript中IE浏览器不支持NEW DATE()带参数的解决方法 (转)
- We(http://www.newshoesweb.com) mainly offer all kinds of Nike/Jordan/Adidas brand sports shoes.Also,We sell some brand hats,bags
- JavaScript Date 对象的异常现象-new Date('0001-01-01 00:00:00')
- 谈谈JavaScript的New关键字
- Javascript 面向对象编程中的‘new’