How to use new in JavaScript and considerations _javascript Tips

Source: Internet
Author: User
Original: JavaScript, We hardly new Ya--douglas Crockford.
http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/
Reference
JavaScript is a prototype based language, but it has a new operator that makes it look like a classic face object language. That also confuses programmers, leading to problematic programming patterns.

In fact, you never need to use the new Object () in JavaScript. Use the literal form {} to replace it.

Similarly, do not use the new Array () and replace it with a literal []. Arrays in JavaScript do not work like arrays in Java, and using Java-like syntax can only confuse you.

Similarly, you don't need to use new number, new String, or new Boolean. The use of these will only produce unwanted type encapsulation objects. Just use the simple literal volume directly.

Do not use the new function to create a function object. Use function expressions better. Like what:

Frames[0].onfocus = new Function ("Document.bgcolor= ' Antiquewhite '")

A better way to do this is to:

Frames[0].onfocus = function () {Document.bgcolor = ' antiquewhite ';};

The second form allows the script compiler to see the body of the function faster, so the syntax errors are detected faster. Sometimes programmers use the new function because they don't understand how internal functions work.

Selobj.onchange = new Function ("dynamicoptionlistobjects[" +
dol.index+ "].change (This)");

If we allow strings to function as bodies, the compiler cannot see them. If we use string expressions as function bodies, we also do not see them. The better way is not to blindly program. By making a function call that returns a value as a function, we can explicitly pass the value we want to bind by value. This allows us to initialize a series of Selobj objects in the loop.

Selobj.onchange = function (i) {
return function () {
Dynamicoptionlistobjects[i].change (this);

};
} (Dol.index);

Using new directly on a function is never a good idea. For example, the new function does not offer any advantage in constructing a newer object.

MyObj = new function () {
This.type = ' core ';
};

The better way is to use object literals, which are lighter and quicker.

MyObj = {
Type: ' Core '
};

If we need to create an object that contains methods that require access to a private variable or function, the better way is still to avoid using new.

var foo = new function () {
function processmessages (message) {
Alert ("message:" + message.content);
}
This.init = function () {
Subscribe ("/mytopic", this, processmessages);
}
}

By using new to invoke a function, the object holds a meaningless prototype object. This will only waste memory and will not bring any benefits. If we don't use new, we don't have to maintain a useless prototype object in the object chain. So we can use () to correctly invoke the factory function.

var foo = function () {
function processmessages (message) {
Alert ("message:" + message.content);
}
return {
Init:function () {
Subscribe ("/mytopic", this, processmessages);
}
};
}();

So the principle is simple: the only place where you should use the new operator is when you call an old constructor function. When you call a constructor function, you are forced to use new. Sometimes you can come to new, some time or not.
Reference
Note Original: http://www.uiplanet.com/taobao/2007/05/15/%e4%bd%a0%e7%9c%9f%e7%9a%84%e4%bc%9a%e5%86%99javascript%e5%90%97%ef%bc%9f/
You can refer to the following articles in English: http://msdn.microsoft.com/msdnmag/issues/07/05/JavaScript/default.aspx?loc=en#S6
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.