Inheritance features in JavaScript

Source: Internet
Author: User

    • Object-oriented programming language classification
      1. Class-based inheritance: such as java,c++
      2. Based on prototype inheritance: JavaScript, for example
    • The prototype inheritance is dynamic, and the method added to the prototype takes effect immediately, even if the object instance exists before the new method is given to the prototype, and the new method can be obtained.
    • JavaScript has no block scope, so unlike the C language to delay declaring variables as much as possible, in JavaScript, you should place all the local variable definitions at the beginning of the function definition
    • Closures are external calls to a function to return an object, although the returned object itself has access to the context within which it was created, but it can be hidden when exposed to external callers
    • JavaScript can have closures as a feature, thanks to:
      1. Functions can be nested, and outer functions can be nested inside a function
      2. The function scope, the outer function defines the variable, the inner layer function can be accessed, which in turn does not establish
      3. Outside the function block, you cannot access any local variables defined inside the function block
      4. A function can return a simple type, a literal object, or a function
      5. To successfully use closures, the outer layer must first have a function that eventually returns an authorization function
    • The most important thing to do with closures is that you want to emulate the operation of a proprietary private property (method) similar to a class in Java, which is significant in object-oriented and module development
    • Early JavaScript, just as a scripting language that runs in a browser environment, supports dynamic client interaction, so global variables are not a big problem
    • But as JavaScript is increasingly used in front-end frames (such as Ember), and even at the service end (such as Nodejs), object-oriented and module development is an inevitable choice
    • When there are many parameters in the constructor, you can use the JSON-defined object literal method to pass in, and the benefit of constructing the object is
      1. Scalability of the parameters
      2. Do not rely on parameter order
      3. Allow parameter default values
    • In the JS inheritance mode on the road, along the way, we have the following two ways
      1. Using the new constructor + Specify the prototype prototype object, but this way can not really implement data hiding and encapsulation, therefore can not do to protect object integrity
      2. Construct parameters + closures through JSON object literals, because closures can really hide private data and attributes, so that object integrity can be protected

Inheritance features in JavaScript

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.