JavaScript variable declaration, hoisting mechanism, object-oriented

Source: Internet
Author: User
Tags uppercase letter

Following non-compliance may cause unknown errors

1. The curly brace that represents the beginning of the block, not another line.

2. Do not omit the semicolon at the end of the sentence

3. Do not use the WITH statement, you can reduce the writing of the code, but will cause confusion.

4. Do not use the "equality" (= =) operator, only the "strict equality" (= = =) operators.

= = value types on both sides of the time, to first type conversion, and then compare, resulting in a lot of unexpected situations.

= = = does not do type conversion, different types must vary.

0 = = "//True1 = TRUE//True2 = TRUE//False0 = = ' 0 '//TrueFalse = = ' false '//Falsefalse = ' 0 '//true" \t\r\n "= = 0//True

5. All variable declarations are written on the function's head. All functions are defined before use

Because JavaScript's function declarations and variable declarations have a hoisting mechanism, declarations are always promoted to the top of the scope.

var a = 10; (function () {    alert (a);//undefined    var a = 20;}) ()//equivalent to: var a = 10;var F = function () {    alert (a);//undefined    var a = 20;} F ()//equals: var a = 10;function xx () {    alert (a);//undefined    var a = 20;} XX ()//equivalent: (function () {    var a;//is here to the variable hoisting, first declare    alert (a);    A = 20;//re-assignment}) ()

Note: var f = function () {} is not the same as functions f () {}.

f ();//error var F = function () {    alert ("Hello");} The statement is equivalent to: Var f;f (); f = function () {    alert ("Hello");}

  

var B = (function () {    return;  } ()) and (function () {    var a;//here the variable hoisting, first declare    alert (a);    A = 20;//re-assignment}) ()

  

Safe sandbox mode

var f = function foo () {    return typeof Foo;}; typeof foo;//"undefined" f ();//"function"

Function name Foo is only useful within the scope

The namespace mechanism is not provided in JavaScript itself, and in order to avoid the pollution of global space by different functions, objects, and variable names, it is common practice to create a unique global object for your application or library, and then add all the methods and properties to this object

function person (name, age) {    this.name = name;    This.age = age;    This.sayname = function () {        alert (this.name);    }} Person (1, 2) alert (name)//We can use this in the inside of the function. XXX and Var to declare variables. The difference is the use of this. The variables declared by XXX can be accessed externally. Variables declared with Var are not directly accessible outside the function due to the protection of the scope of the function.

In JavaScript, there are no keywords that show the declaration of a private member, and so on. Therefore, in order to realize the encapsulation/information hiding, we need to start from another idea. We can use the concept of closures to create methods and properties that allow access only from within an object to meet the requirements of encapsulation.

Basic way in general, we have learned three ways to achieve the purpose of encapsulation. Use this. XXX to declare a variable, and then declare GetXXX, SETXXX, and so on value, assignment method. Use THIS._XXX to declare a variable, and then declare GetXXX, SETXXX, and so on, the method of assigning values. Use the concept of "function scope" to do this.

  

var book = function (isbn,title,author) {  this.setisbn (ISBN);  This.settitle (title);  This.setauthor (author);}; Book.prototype = {  setisbn:function (ISBN) {    this.isbn = ISBN;  },  getisbn:function () {    return THIS.ISBN;  },  settitle:function (title) {    this.title = title;  },  gettitle:function () {    return this.title;  },  setauthor:function (author) {    this.author = author;  },  Getauthor: function () {    return this.author;  }};/ /encapsulation implemented using this method, although the accessor and evaluator are implemented to protect private properties. However, in practice, private properties can still be accessed externally, so the package is not implemented in the fundamental sense.

  

var book = function (isbn,title,author) {  this.setisbn (ISBN);  This.settitle (title);  This.setauthor (author);}; Book.prototype = {  setisbn:function (ISBN) {    this._isbn = ISBN;  },  getisbn:function () {    return THIS._ISBN;  },  settitle:function (title) {    this._title = title;  },  gettitle:function () {    return this._title;  },  setauthor:function (author) {    this._author = author;  },  Getauthor:function () {    return this._author;  }};/ /using this method is similar to the first, except that different naming is used to protect the use of private properties. However, it still does not implement encapsulation from the actual application.

  

var book = function (newisbn,newtitle,newauthor) {  var isbn,title,author;  This.setisbn=function (NEWISBN) {    ISBN = newisbn;  };  This.getisbn=function () {    return ISBN;  };  This.settitle=function (newtitle) {    title = Newtitle;  };  This.gettitle=function () {    return title;  };  This.setisbn=function (newauthor) {    author = newauthor;  };  This.getisbn=function () {    return author;  };} Because variables declared in JavaScript functions are scoped, using this approach avoids the direct access to private properties externally. Basically achieve the required content of the package.

  

Whenever a new function is created, a prototype property is created for the function based on a specific set of rules, which points to the prototype object of the function. Declare that the properties of the function Book,book prototype point to the same as this in book? http://www.cnblogs.com/sitemanager/p/3535904.html

The constructor name that must be used in conjunction with new should start with an uppercase letter. JavaScript will not have any compilation errors or run errors thrown when new is omitted. Forgetting to add new causes bad things to happen (such as being treated as a normal function ), so the capitalization constructor name is the only way we can try to avoid this happening.

JavaScript variable declaration, hoisting mechanism, object-oriented

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.