Javascript study NOTE 2: javascript prototype + object literal volume

Source: Internet
Author: User

1. When you use for-in to traverse an object (for-in is better not to traverse an array, but to traverse an array, you are advised to filter out the interference of the prototype chain of the object.

If (Object. prototype. clone = 'undefined') {Object. prototype. clone = function () {}}for (var key in MyObject) {if (MyObject. hasOwnProperty (key) {// indicates that the original type chain exists. Some objects or attributes in the prototype chain may conflict with the attributes of the objects we are traversing, therefore, the prototype chain should be filtered out .}}

2. To add a prototype property (method), you must use the following method: Object. prototype. Property/method =.../function (){}.


3. Implicit global variables and explicit global variables: the implicit global variables are the global variables declared in the function (funtion), and the explicit global variables are not the global variables declared in the function.

3. avoid implicit type conversion in the Code: true is returned when "false = 0" is judged, which is not logical, in this case, "false = 0" is used to avoid implicit conversion of this type.

4. do not use eval (): this function can execute any string as a javascript code snippet. When the code to be discussed is prepared in advance, there is no reason to use eval ().
If the code is dynamically generated during runtime, there are other methods to implement the corresponding functions.

Example: var a = eval ('192, 1234 + 100'); // The result is 1810var B = '192, 567 + 100'; // The result is 1234 + 567 alert ();

5. parseInt (): This method parses a string and returns the value. When the string is of the "09" type, the return value is 0 when parseInt ('09') is used, because strings starting with 0 are parsed as octal characters.
In this case, we need to use another parameter to constrain this method. Like this, parseInt ('09', 10) indicates that the string is parsed in decimal format, the returned result is 9;
Of course, if all the values in the string are numerical values, you can use the Number ('09') method to obtain the 9 result, but if the string like "009 Hello" uses Number () the returned result is NaN.
Or use the parseInt () method.

6. "Object literal volume" in javascript ":

  var Person = function(name){this.name = name,this.say = function(){return 'I am '+this.name;}  }

When we use the new constructor, it actually happens internally:

Var Person = function () {// creates an "empty" object. The reference of this object points to the object var this = {}; // you want to add attributes and methods for this "null" object. name = name, this. say = function () {return 'I am' + this. name ;}}

In fact, the above-mentioned this object is actually an empty object, which is inaccurate because this inherits all the members in the Person prototype. Therefore, it is more like the following statement:
This = Object. create (Person. prototype)
In addition, we can return any parameter in the constructor, as long as this parameter is an object.

7. reusable methods in javascript should all be placed in the first prototype.


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.