JavaScript Object-oriented Inheritance, javascript object-oriented

Source: Internet
Author: User

JavaScript Object-oriented Inheritance, javascript object-oriented

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 


Javascript is object-oriented and can reflect its inheritance.

...... JS is only based on object-oriented, rather than the real object-oriented, because the three main features of object-oriented: encapsulation. Inheritance and polymorphism!

Javascript inheritance gets dizzy.

This is a typical JavaScript prototype chain Inheritance Problem.

I. JavaScript object-oriented World View
In JavaScript, all except the five original data types are objects, including function objects;
JavaScript does not have the concept of classes, but only functions and prototypes. Some JavaScript articles will mention classes. This is just a metaphor. Compared with the class-based language (Java, C #), it can be understood as a class, but it is definitely not a class;
JavaScript is not a class-based Object-oriented Inheritance, but a prototype-Based Object-Oriented inheritance. The implementation principle mainly depends on the prototype chain mechanism of JavaScript;

The above are the basic principles of JavaScript. If you understand it, you can continue.

Ii. Differences between functions and objects
// This is a function declaration. According to the above principle, Person is a function object, function Person () {}// this is a direct volume object creation, use {} to directly create an object and assign a value to obj to reference var obj = {};

Iii. Differences between constructors and functions

In JavaScript, apart from functions, there is also a concept called constructor. Do not be fooled by this name. constructor is a function. There is no difference in nature, but it is just an ideological thing. A constructor can distinguish functions from each other directly by name, as shown below:
Function fn1 () {} fn1 (); // The fn1 function is just a call, so it is just a common function Klass () {} new Klass (); // Klass is instantiated using the new operator, so we call it a constructor. // Similarly, if we want fn1 to be instantiated, it can also be called fn1 as the constructor new fn1 ();
However, JavaScript is still used to class-based naming. constructors start with uppercase letters and treat them as a class.

Iv. Differences between function objects and objects

For example, the question of the subject:
// This is a Function object, which is an instance object of the constructor (Function) // Note: The Constructor (function) and the above Function object are written here, function Person () {}// this is an Object. It is an instance Object var obj ={} of the constructor (Object). // At the same time, the constructor (Function) object is a function Object.
Only function objects can be instantiated using the new operator:
New Person (); // This is The new obj () that works; // the JavaScript engine throws an exception "TypeError: object is not a function"

V. Differences between prototype and _ proto _

Prototype: prototype is a prototype object. Only function objects have this property. prototype is also an object.

_ Proto _ is a private attribute of all JavaScript objects. __proto _ is read-only. Both common objects and function objects have the _ proto _ attribute.

_ Proto _ points to the prototype object of its constructor:
// Obj is the Instance Object of the constructor (Object), so console. log (obj. _ proto _ = Object. prototype); // true // Person is the Function of the constructor ...... remaining full text>


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.