Do you really know how to write JavaScript?

Source: Internet
Author: User
Tags array arrays object constructor return string

See the new article on MSDN today: Create Advanced Web applications with object-oriented techniques.

It's been a long time since I've seen such a good article. Last seen is a Douglas crockford JavaScript, We hardly new Ya (I briefly translated the translation after).

Like other articles that teach you how to write JavaScript with object-oriented ideas, the article focuses on the following elements:

    • The object of JavaScript is an associative array.
    • A JavaScript function is also an object.
    • Prototype (Prototype)
    • Closure (Closures)
    • Inheritance/private Property/static method
    • Name space

The author is well written, English is easy to read, there is no uncommon use of words (incidentally, "PPK on JavaScript," the author's English is not flattering). The code used for the example is also appropriate.

In particular, the beginning of the article is very interesting, the author wrote that he and a said has been written for nearly 4 years JavaScript female programmers chat, female programmers think her JS level very good, and later the author found that she does write, but only write, in fact, there is little knowledge of the meaning of JavaScript.

The author wants to use this example to illustrate that many developers with java/c++/c# development experience, when they write JavaScript or switch to the Fed (like me), take it for granted that the idea of standard object-oriented language is applied to JavaScript, but goes astray.

I have a deep understanding of this, I was really involved in an Ajax project and really read the prototype framework of the source code, JavaScript has a completely new awareness.

In short, recommend reading. Attached JavaScript, We hardly new ya translation, translated hastily, there will be no line of writing, the Guests forgive me!

JavaScript's new, long time no see.

Original: JavaScript, We hardly new Ya -- Douglas Crockford.

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.



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.