objects, functions, and inheritance in JavaScript

Source: Internet
Author: User
Tags constructor implement inheritance

1. Objects in JavaScript

JavaScript can be said to be an object-based programming language, why it is object-based rather than object-oriented, because JavaScript itself only implements encapsulation, and does not implement inheritance and polymorphism. Since he is based on the object, then we say the object in JS. Some people say that all of JS are objects, this sentence is not entirely correct. The right side is that he emphasized the importance of the object in JS, the object is ubiquitous in JS, including the function that can construct the object itself is also an object. On the other hand, JS also has some simple data types, including numbers, strings and booleans, null values, and undefined values, which are not objects. So why are these types of values not objects, after all they have methods. So let's see, there are two definitions for the definition of objects in JavaScript.

(1) The object in JavaScript is a mutable set of keying (keyed collections) (this definition comes from chapter III of the sophisticated book)

(2) Objects in JavaScript are unordered (unordered) collection of properties that can contain simple data types, objects, functions, and functions that are stored in an object property are also called methods of this object. (from the ECMA-262 4.3.3) (Note: The attributes described here are those that can be created and accessed in a JS script (which we call the dominant attribute), excluding the internal attributes that the system automatically assigns to the object.

So why is that simple data type not an object, mainly because the methods in the values of these data types are immutable, and the attributes of an object should be able to be changed.

2. The prototype chain in the object [[Proto]]

When each object in JavaScript is created, the system automatically assigns it a prototype attribute [[Proto]], which is used to connect to his prototype object. In JavaScript, an object's inheritance is implemented through [[Proto]] in each object. However, the [[Proto]] property of an object cannot be accessed and modified in JavaScript, as an intrinsic attribute, and is automatically set by the system while the object is created.

When a property of an object is accessed, if the property does not exist in this object, look for the properties of the archetypal object that his [[Proto]] refers to, and return if found, otherwise continue to search along the [[Proto]] chain until the [[Proto]] connection is null.

3, the function is also the object

The function in JavaScript itself is an object (so we often call it a function object), and it can be said that he is the most important object in JS. It is called the most important object, on the one hand he can play the same role as a function in other languages, can be invoked, can be passed in parameters, on the other hand, he is also used as an object's constructor (constructor), can be combined with the new operator to create the object.

Since a function is an object, it necessarily contains all the properties that the object has, including the prototype chain [[Proto]] attribute that the object was created with.

Let's look at what distinguishes a function object from a normal object. As we said earlier, objects are unordered sets of attributes, so what is the difference between the properties of a function and the properties of a normal object? As described in section 13.2 of ECMA-262, when a function object is created, the system defaults to creating two properties [[Call]] and [[constructor]], when the function object is called as a normal function (for example, MyFunc ()), "()" The operator indicates that the [[]] property of the function object is executed, and when he is invoked as a constructor (for example, New Myconst ()), his [[constructor]] property is executed, [[[Cosntructor]] The implementation process we will introduce in the next section. In addition, when a function is created, the system creates a display property prototype for it by default and assigns it a value of

This.prototype = {Constructor:this}

The specific content can take part in the fifth chapter of the seasoned book. This function object's prototype property is also for JS to implement the function as a constructor for inheritance is prepared, but this attribute can be accessed and modified in the JS script. The point to be emphasized here is that you have to distinguish between the [[Proto]] property in the object and the prototype attribute in the function object, I just started to learn because there is no good distinction between these two things, walked a lot of detours.

4, the creation of objects

There are two ways to create objects in JS, one by literal means, such as

var Person = {

“first_name”:’liang’,

‘last_name’:’yang’

}

Another method is to create a

var i = new person (' Liang ', ' Yang ');

In fact, the first way to create the process is equivalent to calling the object constructor to implement, as follows.

var Person = new Object();

Person.first_name = ‘liang’;

Person.last_name = ‘yang’

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.