Tips for creating and calling JavaScript objects

Source: Internet
Author: User
This article mainly introduces how to create and call JavaScript objects. For more information about how to create JavaScript objects, see create JavaScript objects. Therefore, Bing wrote an article about three JavaScript objects created by foreigners. after reading the article, he wrote the code again. I think the method is quite good. I 'd like to share it with you here.

1. create objects using functions:

The code is as follows:


// Define the object
Function Animal (type)
{
This. name = "";
This. type = type;
This. introduction = function (){
Return "My name is:" + this. name + ", I belong to" + this. type;
}
}
Var animal = new Animal ("poultry"); // instantiate the object we created above
Animal. name = "Xiaohong ";
Alert (animal. introduction (); // call its introduction function (at this time, the page will pop up: My name is Xiaohong, and I belong to poultry );

You must be familiar with this method. However, using this method will cause performance loss. Here, we use the new key child to instantiate the object. In fact, the key to new is to do two things. 1. defines an anonymous method (Animal ). 2. call it. This is not as efficient as the method we will introduce next.

2. object literals ):

I don't know if the translation is correct. I will tell you the original article address later. if you are interested, you can read the original article.

The code is as follows:


// Define the object
Var Book =
{
Name: "Dream of Red Mansions ",
Type: "Literature ",
GetAuthor: function ()
{
Return: "I'm a child of Cao Xueqin! ";
}
}
Alert (Book. GetAuthor (); // call the object method. the page displays: I am a child of Cao Xueqin.
Book. name = "dunk"; // modify object attributes
Alert (Book. name); // at this time, the page will pop up: Dunk

I believe that when you see the code, you should understand why this method is more efficient. Because it is equivalent to defining a JavaScript global variable. We can use it directly without instantiating it. However, this looks strange. Then, the solution is coming. Let's take a look at the third method.

III. Singleton using a function ):

It may not be too proper to translate into a singleton mode. Let's look at the Code First:

The code is as follows:


// Define the object
Var Gender = new function ()
{
This. type = "girl ";
This. speaking = function ()
{
Return "I am" + this. type;
}
}
Alert (Gender. speaking ();) // when you use an object, the following page appears: I am a girl.

Let's look at this code. is it very similar to our method? However, it can work like Method 1. Method 1: an object is created once. This method can be used permanently after an object is created. Therefore, this method is similar to the Singleton mode in the design mode.

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.