博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript, We Hardly new Ya
阅读量:5278 次
发布时间:2019-06-14

本文共 2988 字,大约阅读时间需要 9 分钟。

JavaScript is a prototypal language, but it has a
new operator 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 Function to 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 Function is 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 selObj in a loop.

selObj.onchange = function (i) {

return function () {

dynamicOptionListObjects[i].change(this);

};

}(dol.index);

It is never a good idea to put new directly in front of function. For example, new function provides 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 new prefix.

var foo = new function() {

function processMessages(message) {

alert("Message: " + message.content);

}

this.init = function() {

subscribe("/mytopic", this, processMessages);

}

}

By using new to invoke the function, the object holds onto a worthless prototype object. That wastes memory with no offsetting advantage. If we do not use the new, we don’t keep the wasted prototype object 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 new operator is to invoke a pseudoclassical Constructor function. When calling a Constructor function, the use of new is mandatory.

There is a time to new, and a time to not.

转载于:https://www.cnblogs.com/jinweijie/archive/2006/11/20/566619.html

你可能感兴趣的文章
学习进度条--第五周
查看>>
获取spring中所有的bean名称
查看>>
linux常用命令
查看>>
java DecimalFormat
查看>>
简单两步快速学会使用Mybatis-Generator自动生成entity实体、dao接口和简单mapper映射(用mysql和oracle举例)...
查看>>
Spring读书笔记-----Spring核心机制:依赖注入
查看>>
如何挂载阿里云的数据盘
查看>>
block extends include三者的差别跟用法
查看>>
服务器安全
查看>>
系统学习qsort1 尤其partition
查看>>
yield生成器对象返回Fiabs元素 分类: python 小练习 ...
查看>>
HDU 1001 Sum Problem
查看>>
BZOJ 1196 [HNOI2006]公路修建问题(二分答案+并查集)
查看>>
Android学习笔记1:初识框架
查看>>
bzoj 2005
查看>>
杜教筛模板
查看>>
浅谈委托事件
查看>>
装箱问题
查看>>
C++:重载全局new/delete实现跨平台多线程内存检测
查看>>
文法解释修改
查看>>