How to use new in JavaScript and precautions _ javascript skills
Source: Internet
Author: User
Usage and precautions of new in JavaScript: JavaScript, We Hardly new Ya -- Douglas Crockford.
Http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/
Referencing JavaScript is a prototype-based language, but it has a new operator that makes it look like a classic object-oriented language. This also confuses programmers and leads to some problematic programming modes.
In fact, you never need to use new Object () in JavaScript (). Replace it with the literal form.
Similarly, do not use new Array (), instead of literally []. Arrays in JavaScript do not work like arrays in Java. Using Java-like syntax will only confuse you.
Similarly, you do not need to use new Number, new String, or new Boolean. These operations only generate useless type encapsulation objects. Simply use a simple literal.
Do not use new Function to Create Function objects. It is better to use function expressions. For example:
Frames [0]. onfocus = new Function ("document. bgColor = 'antiquewhite '")
The second form allows the script compiler to see the function body more quickly, so the syntax errors in the form will be detected more quickly. Sometimes programmers use new functions because they do not understand how internal functions work.
SelObj. onchange = new Function ("dynamicOptionListObjects [" +
Dol. index + "]. change (this )");
If we use strings as function bodies, the compiler cannot see them. If we use string expressions as function bodies, we will not see them either. A better way is not to program blindly. By creating a function call whose return value is 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 a loop.
SelObj. onchange = function (I ){
Return function (){
DynamicOptionListObjects [I]. change (this );
};
} (Dol. index );
Using new for a function is never a good idea. For example, the new function does not provide any advantages for creating new objects.
MyObj = new function (){
This. type = 'core ';
};
The better way is to use the object literal, Which is lighter and faster.
MyObj = {
Type: 'core'
};
If the methods contained in the objects to be created need to access private variables or functions, the better way is to avoid using new.
Var foo = new function (){
Function processMessages (message ){
Alert ("Message:" + message. content );
}
This. init = function (){
Subscribe ("/mytopic", this, processMessages );
}
}
By calling a function using new, the object will hold a meaningless prototype object. This will only waste memory and will not bring any benefits. If we do not use new, we do not need to maintain a useless prototype object in the object chain. So we can use () to call factory functions correctly.
Var foo = function (){
Function processMessages (message ){
Alert ("Message:" + message. content );
}
Return {
Init: function (){
Subscribe ("/mytopic", this, processMessages );
}
};
}();
So the principle is very simple: the only place to use the new operator is to call an ancient constructor function. When calling a constructor function, it is mandatory to use new. Sometimes you can try new ones. Sometimes you can try again.
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/
Can refer to the following article, English: http://msdn.microsoft.com/msdnmag/issues/07/05/JavaScript/default.aspx? Loc = en # S6
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.