JavaScript Object-oriented

Source: Internet
Author: User
Tags prototype definition throw exception

Object-Oriented is a method of understanding and abstraction of the real world. It's just abstract. Of course, there are also object-oriented JavaScript.

1. Methods for creating objects

(1) General ways to create objects

var New Object ();

This creates an object, but the object is also typed (of course, the type is defined by itself), so that objects created are of type object, and in use, it is difficult to distinguish between different types of objects.

(2) Creating an object from a constructor function

function Person (name) {            this. Name=name;                           // Add Property   
......
}

This creates a person object, which contains the properties of the person object, in fact, similar to C #

(3) Creating objects from object literals

        var p1 = {            ' name ',            ' 163.com ',
function () {alert (' t. age);} };

An object can be thought of as a collection of key-value pairs that can be iterated with for......in (for in loops equivalent to a foreach loop in C #)

2. Prototype objects

Prototype object: An incomprehensible concept, presumably a _proto_ attribute of a prototype definition, to point to a previous layer of object, specifying: http://www.cnblogs.com/shuiyi/p/5305435.html

The key is the role of the prototype object:

--Create a prototype chain to find the properties, or methods, of the previous layer through the prototype chain.

Principle: When getting the properties of an object, the system first detects whether the object contains the property directly, and does not include a re-prototype to find the property.

In this way, the implementation of inheritance , in JS, there is no concept of class, inheritance is through the object and objects implemented between

var p1=New person ();   MAN.PROTOTYPE=P1;   // realizes man inherits man

3. Closed Package

Concept: A function is defined inside a function, and an intrinsic function can access a variable within the scope of an external function, which is called a closure
Effect: Changing the scope of a variable to simulate object-oriented

4. This pointer

This is important to use in JavaScript.

This refers to the summary of the objects:

(1) Using this in the global execution environment is the window object

(2) If the function is not obvious as a property of a non-window object, but simply defines a function, regardless of whether the function is defined in another function, this function's this still represents the Window object (because it is not registered to the object, so it shows the Window property)
If the function is explicitly used as a property of a non-window object, then this in the function represents the object

(3) When a function is called by the new operator, the function is treated as a constructor, which points to the object created by the constructor

5. Error Object

    var New Error (' custom error ');      // Create an instance            of the error class throw custerror;                              // Throw exception

Of course, there are a lot of objects in the JavaScript object, there are methods, but beginners are basically not involved in the understanding of some basic.

JavaScript Object-oriented

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.