Home > Web Front-end > JS Tutorial > How to use new in JavaScript and what to pay attention to_javascript skills

How to use new in JavaScript and what to pay attention to_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 19:13:18
Original
1103 people have browsed it
Original text: JavaScript, We Hardly new Ya - Douglas Crockford.
http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/
Quote
JavaScript is a prototype-based language, but it has a new operator that makes it look like a classic object-oriented language. That also confuses programmers and leads to some questionable programming patterns.

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

Similarly, do not use new Array(), but use literal [] instead. Arrays in JavaScript don't work like arrays in Java, and using Java-like syntax will just confuse you.

Similarly, there is no need to use new Number, new String, or new Boolean. Usage of these just produces useless type-encapsulated objects. Just use simple literals.

Do not use new Function to create function objects. It's better to use function expressions. For example:

frames[0].onfocus = new Function("document.bgColor='antiquewhite'")

A better way to write it is:

frames[0] .onfocus = function () {document.bgColor = 'antiquewhite';};

The second form allows the script compiler to see the function body faster, so syntax errors in it will be detected faster come out. Sometimes programmers use new Function because they don't understand how intrinsic functions work.

selObj.onchange = new Function(”dynamicOptionListObjects[”
     dol.index ”].change(this)”);

If we use a string as the function body, compile The device cannot see them. If we use string expressions as function bodies, we also can't see them. A better approach is not to blindly program. By making a function call that returns 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 directly on a function is never a good idea. For example, new function provides no advantages for constructing new objects.

myObj = new function () {
this.type = 'core';
};

A better way is to use object literals, which are lighter and more Fast.

myObj = {
type: 'core'
};

If the object we need to create contains a method that needs to access private variables or functions, the better way is still Avoid using new.

var foo = new function() {
function processMessages(message) {
alert(”Message: ” message.content);
}
this. init = function() {
                                                      use using new to call the function,                                     through outsencermbps outmb straightps out  out  out out out out out right outoleole right outole outole out out out out out out out‐‐‐‐‐‐‐‐‐‐‐‐‐‐''' Prototype object. This just wastes memory without any benefit. 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 call the factory function correctly.

var foo = function () {
function processMessages(message) {
alert(”Message: ” message.content);
}
return {
init: function () {
                                                                                                                                        The place where you get to the new operator is when you call an ancient constructor function. When calling a constructor function, it is mandatory to use new. Sometimes you can come in for a new one, but sometimes it’s better not to.




Quote

Note Original text: http://www.uiplanet.com/taobao/2007/05/15/ Can you really write javascript? /
You can refer to the following article, in English: http://msdn.microsoft.com/msdnmag/issues/07/05/JavaScript/default.aspx?loc=en#S6

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template