JS Advanced. 02 Object-oriented, object creation, constructors, custom constructors, prototypes

Source: Internet
Author: User

Three main features of object-oriented:

  

    1. Packaging

A) put some properties and methods into one object

2. Inheritance

A) The inheritance in JS refers to:

      1. An object has no methods and properties, and another object has
      2. Take the properties and methods of another object, take it yourself, and that's the inheritance.

b) Mixed-type inheritance for ... in

1. The properties and methods of the parent class are all

3. polymorphic

A) JS is not reflected in the corresponding

b) More common in strongly typed languages

c) The variable of the practical parent class accepts the object of the child class

D) The property method of the parent class is shared for all subclasses

How objects are created

1. Create an Object

Four methods:

1, the literal creation

Create one with one and cannot be reused. Can cause code Shanyu, waste of resources

2. Built-in constructors create objects

1. Built-in constructors create objects

1. var p = new Object ();

2.var arr = new Array ();

3. Created objects are empty objects, to manually add properties, resulting in code duplication

3, Package simple factory function (not recommended)

        

function    creatobj (name, age) {    var obj = {            boj:name;            Obj:age;            Sayhello:function () {                                    console.log (');                            }                    }             return obj;}

4. Custom constructors

       

function Porple (data1,data2) {                this. xx = data;        } 
What is a constructor used for?

In JavaScript, constructors are used to add properties to an object and initialize properties.

The creation process of an object
var p = new Person();

Take the above P object creation as an example:

    1. Create an object with the New keyword first, similar to the {} one used, and the object created at this time is a "no member" object. Here are two points to note:

      • newobject created using the keyword, the type of the object is the name of the function that created the constructor used by this object
      • Using the {} Create object, the type of the object must be the Object equivalent of using thenew Object()
    2. To initialize a member with a constructor

      • At the beginning of the constructor call, there is an assignment operation, which is to letthis = 刚创建出来的对象
      • In the constructor, this it represents the object that was just created
    3. Add members to an object in the constructor, using the object's dynamic properties

Custom constructors

function Porple (DATA1,DATA2) {

this.xx = data;

}

    1. Concept: Used to instantiate an object and assign a value to the initialized object
    2. Constructor name first letter uppercase
    3. Constructors are typically used with the new keyword
    4. The constructor return value defaults to the newly created object, if the return value is set manually

A) The return value type returns the created object if it is not of type Object

b) Returns an object if it is of type Object

A) object literal {}, creating object

b) Custom constructors, creating objects

    1. Constructors are also functions, which are commonly used to initialize objects
    2. New to create an object
    3. Constructors are used to initialize functions
    4. To capitalize the constructor name,

c) The execution process of the constructor function

    1. Create an object using the New keyword
    2. Call the constructor to assign the newly created object to the This keyword of the constructor
    3. Within the constructor utility this adds a member to the newly created object
    4. The newly created object is returned by default
    5. If you return a null value, or return a basic type of data, the newly created object is still returned

A) return undefined and Null both return new objects

    1. If the return is of type object, it will return the objects after return
    2. Function name parentheses are called functions
    3. Do not write parentheses is to assign the whole code of the function to another variable
    4. If the function is called without a constructor argument, the constructor does not create a new object, and this will point to the window, and the added property will be added to the window
    5. If the constructor has no arguments, it can be called without writing parentheses

Object members: Properties and methods

Prototype
    • Every function, when defined, will have an object associated with it being created.
    • Each object created by the constructor is associated with the default and the mysterious object of the constructor function.
    • When a method is used to access a property or method, the property and method are found within the current object
    • If it is not found within the current object, go back to the mysterious object it is associated with and look inside

    1. Accessing the prototype of a constructor

A) constructor. prototype

2 . Constructor . prototype. New Property = Property value

3. The attributes that you and the prototype have, take precedence over your own

3. Create an object called an instantiated object

A) The process of creating object creation objects from a constructor function

4. Example

A) An instance of this constructor is an object that is instantiated by a constructor function

5. Practical methods of prototyping:

A) Add attributes to the prototype object using the object's dynamic characteristics

6. Replace the prototype object directly

Porple.prototype = {

Name: ' Jack ',

}

A) problems with direct substitution of prototype objects

      1. The prototype of the object created before the replacement prototype and the prototype of the object created after the replacement prototype are not the same
      2. Replace is not modified

b) When the point syntax is assigned to a property, the prototype is not searched.

c) when assigning a value using Point syntax , if the property does not exist in the object, it will be added to the object and will not be found in the prototype

d) If the attribute of the prototype total score is a property of a reference type, then all objects share the property, and an object modifies the reference

A member of the type, all objects will be modified

E) Properties are not typically placed in the prototype

7. Accessing prototypes through constructors

A) constructor. prototype

8. Accessing prototypes through objects

A) object. __proto__ // not recommended

9. Constructor: Properties of the prototype object

JS Advanced. 02 Object-oriented, object creation, constructors, custom constructors, prototypes

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.