Objects and prototypes in JavaScript (1)

Source: Internet
Author: User
[Guide] We know in factory mode that to create an object, we can use the following code: varusernewObject (); create an object user using the new operator. name is on the triangle lake; Add the property user to the object. age22; user. address Hubei Wuhan; alert (us... one factory Model

We know that to create an object, we can use the following code:

Var user = new Object (); // use the new operator to create an Object user. name = 'reading at triangle river'; // Add attribute user to the object. age = 22; user. address = 'hubei Wuhan '; alert (user. name + "" + user. age); // return 'reading at the triangle Lake in Wuhan, Hubei'

Using this method to create objects is simple and intuitive, and is also the most basic method for creating objects in JavaScript. However, there is a problem. If we need to create multiple objects, I have to write a lot of repeated code. For example, if we want to create another object user1, We have to re-write the above Code. This is not suitable in the actual development process. If there are too many objects, the amount of code will be greatly increased.

To solve this problem, we can use a method called factory mode to solve the problem of generating a large number of repeated code for instantiated objects.

Function create (name, age) {var obj = new Object (); obj. name = name; obj. age = age; obj. run = function () {return this. name + ''+ this. age ;}; return obj ;}var obj1 = create ('zxc ', 10); // The First Instance var obj2 = create ('cxz', 20 ); // The Second Instance alert (obj1.run (); alert (obj1.run ());

From the code above, we can see that the factory model solves the problem of a large number of duplicate codes in the instantiation era, but there is another problem, that is, the recognition problem, we simply cannot figure out which object they actually belong. For example

alert(typeof obj1); //Objectalert(obj1 instanceof Object); //true

The code above indicates that box1 is an Object, but we cannot know which Object was created.

2. Constructor (constructor)

To solve the problem above, we can use the constructor to create an object. The only difference between a constructor and a common function is that the calling method is different. However, constructors are also function numbers.

Function User (name, age) {// constructor mode this. name = name; this. age = age; this. run = function () {return this. name + ''+ this. age ;};}

You can use the new operator to create an object:

var user1= new User('ZXC', 25);var user2= new User('CXZ', 22);

Now we can check whether user1 or user2 belongs to the User.

alert(user1 instanceof User);//true 

The constructor solves the problem of repeated instantiation and object recognition.

The process of executing the constructor is as follows:

1. When the new Constructor () is executed, the new Object () is executed in the background ();

2. Assign the constructor scope to the new object.

3. Execute the code in the constructor;

4. The background directly returns the new object.

Next, let's look at the internal function problems of the constructor. If we execute the following statement:

Alert (user1.run = user2.run); // The returned result is false.

The returned result is false, which indicates that the method is also a reference address. If we create multiple objects again, the methods in each object will open up new space in the memory, which will waste a lot of space. To solve this problem, we need to share instance attributes or methods. The next article will continue to discuss and solve this problem.

The above is the content of the object and prototype in JavaScript (1). For more information, see PHP Chinese website (www.php1.cn )!

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.