JavaScript-object-oriented creation and javascript object-oriented

Source: Internet
Author: User

JavaScript-object-oriented creation and javascript object-oriented

Speaking of object-oriented, we have learned Vb and C # before, from abstraction to encapsulation to inheritance and polymorphism. The object-oriented world is really fun, but object-oriented in JavaScript is somewhat special, because there is no class in it, and the object creation method is also different.



There are four ways to create an object: create with the new Keyword, create with the literal, create with the factory method, and create with the constructor.

1. Use the new Keyword

<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> var box = new Object (); // create an Object box. name = 'lil'; // create a name attribute and assign a value to box. age = 100; // create an age attribute and assign a value to box. run = function () {// create a run () method and return the return value "return this. name + this. age + 'running... ';}; alert (box. run (); // value of the output attribute and method </span>
2. Create with literal
<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> var box = {name: 'handsome guy ', age: '25', run: function () {return this. the age of name + "is" + this. age} alert (box. run (); </span>
3. Create with custom functions (factory Mode)
<span style="font-family:KaiTi_GB2312;font-size:18px;">function createObject(name,age){var obj=new Object();obj.name=name;obj.age=age;obj.run=function(){return this.name+this.age;};return obj;};</span>

4. Create with Constructor

<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> function Box (name, age) {this. name = name; this. age = age; this. run = function () {return this. name + this. age + 'Look at me ...... ';};}; </span>
4. Comparison of the four methods

Each of the four methods has its own advantages and disadvantages. Compared with the literal method, the new keyword is easier to add attributes and methods when creating objects using the literal method. However, when creating more similar objects, the above two methods become cumbersome, because it is too difficult to add attributes and methods after the objects are created one by one, at this time, the factory mode method comes out. The factory mode method solves the problem of creating multiple similar object declarations, but it cannot distinguish the object from which the instance belongs, as shown in the following example.

<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> var box = createObject ('handsome guy ', 20); alert (box1 instanceof Object ); // return true, which can only be determined from the Object </span>
There is a problem. There is always a solution! At this time, the method for creating objects in the constructor is on stage. The constructor method solves the problem of repeated instantiation and object recognition. The Code is as follows.
<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> var box = new Box ('beauty', 20); alert (box instanceof Box ); // returns true, which clearly identifies a box from a Box </span>
The constructor method is compared with the factory mode method. The constructor method does not display the creation Object (no var box = new Object () and directly assigns the attributes and methods to this Object, there is no Return statement (Return obj ). When using constructor to create objects, these useless things run automatically in the background. Isn't it very effort-saving?
5. Differences between constructors and common functions

Statement: the initial letter of the constructor must be capitalized for better differentiation.

Call: use the new keyword to call the function. var box = new Box ('beauty', 20 );
Summary

The four methods of declaring object creation come along with the upgrading of the problem. Here I think of a saying in Jin Yong's novels: "Heroes come with the Times ", the problem of ing to object creation arises as a result of the problem.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.