JavaScript-Object-oriented and prototype

Source: Internet
Author: User

When it comes to object-oriented, we've learned about VB and C #, from abstraction to encapsulation to inheritance and polymorphism, and the object-oriented world is really fun, but the object-oriented in JavaScript is special, because there are no classes, and objects are created differently.



There are about four ways to create objects: Create with the new keyword, create with a literal, create using a factory method, create with a constructor.

1. Use the New keyword

var box = new Object (); Create an object box.name = ' Lee '; Create a Name property and assign a value of box.age = 100; Create an Age property and assign a value of Box.run = function () {//Create a run () method and return a value of return this.name + this.age + ' running ... ';}; Alert (Box.run ()); Values for output properties and methods
2. Create with literal
var box ={name: ' Handsome ', Age: ' + ', run:function () {return this.name+ "is +this.age}}alert (Box.run ());
3. Create using Custom Function (Factory mode)

function CreateObject (name,age) {var obj=new Object (); Obj.name=name;obj.age=age;obj.run=function () {return this.name +this.age;}; return obj;};

4. Use the constructor function to create

function Box (name,age) {this.name=name;this.age=age;this.run=function () {return this.name+this.age+ ' look at me ... ';};};
4, four ways of comparison

Four ways to create objects each have pros and cons, the new keyword and the literal way to compare, using the literal way in the creation of the object when the addition of properties and methods is relatively simple and convenient. But for creating more similar objects, the above two methods become cumbersome, because the object needs to be created after the addition of properties and methods too cumbersome, this time the Factory mode method is out, using the Factory mode method, to solve the creation of a number of similar object declaration problems, However, it does not differentiate which object the instance belongs to, as shown in the example below.

var box = CreateObject (' Handsome '); alert (box1 instanceof Object); Returns true, which can only be judged by the object that it belongs to
Problem arises, there is always a solution! At this point, the constructor creates the object's method on the stage, using the method of the constructor, that is to solve the problem of repeated instantiation, and solve the problem of object recognition. The code is as follows.

var box= new box (' Belle ', +); Alert (box instanceof box); Returns true, clearly recognizing that box is subordinate to box
The constructor method is compared with the Factory mode method, the constructor method does not display the creation object (no var box = new Object ()), the property and method are assigned to the this object directly, and there is no return statement (return obj). These are not the things in the use of the constructor to create objects when the background automatically run, is not very good?
5. The difference between a constructor and a normal function

Declaration: The initial letter of the constructor must be capitalized, and the main purpose is to make a good distinction.

Invoke: Call with the new keyword, var box = new box (' Beauty ', 20);
Summary

Four ways to declare the creation of objects layers of progressive, are accompanied by the escalation of the problem arises, here I think of Jin Yong's novel in the words "heroes are produced with The Times", mapping to object creation this problem, the method is accompanied by problems arising.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JavaScript-Object-oriented and prototype

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.