Js learning notes (7) Objects

Source: Internet
Author: User

1. Object Creation

Use the new Keyword, such as var a = new Object (); // creates an empty Object.

A direct object volume is used, such as var a ={}; // an empty object is created.

You can also Initialize an object when directly creating an object, such as var a = {x: 100, y: "hello "}

The attributes of an object can be dynamically created or added, for example, a. z = 10000 // a new attribute z is created for object.

2. Constructor

Constructor can be understood as a class in other programming languages. when instantiating this class, you must use the new operator.

Function Rectangle (w, h ){

This. width = w; // class attribute. this refers to the specific object during instantiation.

This. height = h;

This. area = function () {return this. width * this. height} // Class Method

}

Var a = new Rectangle (2, 3); // instantiate an object. Parameters in brackets can initialize this object.

Var B = new Rectangle (); // instantiate another object using the same class (that is, constructor)

3. Object Methods

A method is actually a function. The difference is that a method can use the this keyword, while a common function generally does not.

HereThis keyword refers to the object that calls this method.(Of course, the premise is that this object has this method), so it can be understood as a function (whether it is a method function or a common function) once this keyword exists, then it refers to the object that calls this function. In fact, we can regard a common function as a windows Global Object method. Therefore, it is used as a normal function Runtime (that is, not an object (except a windows Object) method) this indicates a windows Object.

4. Prototype objects and inheritance

Each object has an original object, and the object inherits all attributes or methods of its prototype object. The object prototype is defined by the constructor. The prototype of the object is referenced by a. prototype (a is a constructor)

When reading an attribute of object a, the system first checks whether a has defined this attribute. If not, it checks whether a. prototype has defined this attribute in its prototype. When an attribute defined in object a is changed but not defined in its prototype (such as. the x attribute in x is defined in the prototype of object a, and is not directly defined in object a). If. the value of x does not affect the value of the x attribute in its prototype (because if the value in the prototype changes, it may affect a lot of objects, instead of the current object), the system automatically adds an x attribute to object a (equivalent to overwriting the value of the x attribute in the prototype object ).

Even if attributes are added to the prototype object after they are created, the object can inherit these attributes.

Suppose we define a constructor a (), then its prototype object is. prototype. We can add properties or methods for it, such as. prototype. x = 10000, then all objects instantiated with constructor a can inherit the property of x.

In fact, constructors and object prototypes simulate classes and inheritance in other programming languages.

5. prototype and internal class

Not only do user-defined classes have prototype objects, but internal classes such as String and Date have prototype objects. You can also assign values to them. For example, the following code defines a new method, it applies to all String objects.

String. prototype. a = function (){...} // Define a method for the String prototype object

Var m = "hello"; // create a string

M. a (); // The string inherits the () method.

6. When naming a class (in js, It is a constructor), it starts with an uppercase letter. when instantiating a class object, it starts with a lowercase letter.

7. Objects used as correlated Arrays

Object Attributes can be referenced not only by vertex operators, but also by arrays. The following two are equivalent:

A. x

A ["x"]

It is more flexible to reference object attributes in the form of arrays, because the attribute name in [] is a string type, so it can be replaced by a variable, that is to say, you can dynamically reference different object attributes by changing the value of the variable.

 

8. attributes and methods of Objects

Each object has the constructor attribute to represent the constructor that instantiates the object. For example, if the constructor () creates an object o, the property o. constructor represents Complex.

The toString () method of an object returns a string that represents the type or value of the object to be called. This method is called when an object needs to be converted to a string. Different objects return different content using toString (). For example, an array element list is obtained from the array, and the function obtains the original code of the function.

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.