JS OOP < a > Create object, Constructor (class)

Source: Internet
Author: User

JS In addition to the basic type, is the object. Can be said in JS everywhere objects.

Since JS is a weak language, it is easy to confuse object and class in the process of writing objects and constructors .

Object ( object ): The generic object is declared by the var keyword.

class ( constructor ):js In addition to declaring a function, you can also declare the constructor. Therefore, the constructor is a special function.

To create an object:

There are two common ways to create objects.

(1) object literal

var obj = {a:1, b:2};  // Output 1

Object literals can also be nested within objects created by objects

var // Output 3

Literal construct object function

 var  obj = {a:  1 b:  2 //  Create object function   add () { return  this . A + this  .B; },  //  function override   add (x, y) { return  x + Y; }};obj.add (OBJ.A,OBJ.B);             //  output 3  obj.add (); //  output 3  

Note: The object literal is created not by Class (class) But by object (object), so the object created by the literal cannot be used as class to go to new.

var obj = {a:1, b:2};  var New // Error

(2) The constructor creates the object. (class New Object)

The constructor creates the object, creating the constructor (class) first. This is in the same vein as the class new object in other languages.

To create a constructor:

1.function Creation Constructor

function // Constructors var new obj ();     // Creating Objects

The function in JS can also be used as a constructor.

2. Constructor creating a constructor

 function   obj () { this . Name = "Obj_name" ;         this . From = "Obj_china" ;  this . Say = function  () { Console.log ("My name is" + this . _name + "I ' m from" + this  .from); }}  var  obj_1 = new   obj ();  Obj_1.say ();  //  output "My name is Undefinedi ' m from Obj_ China " 

Constructor creating a constructor is a pre-set of properties and functions with the This keyword for use by objects under class.

constructor is similar to a factory, and the drawback is that every time you create an object, you call the factory and waste memory.

3.prototype Create a constructor

    function obj () {}     = "Obj_name";     = "Obj_hobby";     New Array ();     var New obj ();     var New obj ();    Obj_1.arr.push ("one");    obj_2.arr[0]    // output One

Disadvantage once you modify the prototype properties, the properties of the objects on the prototype chain are modified.

The prototype constructor defines properties and functions through prototype, and each object created by the prototype constructor is shared with these properties and functions defined by prototype.

JS OOP < a > Create object, Constructor (class)

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.