Easily win JavaScript (3)-create an object, constructor, and member

Source: Internet
Author: User

This section describes how to create your own objects, how to create constructor methods, and how to create public, private, privileged, and static members.

How to create your own objects: Let's talk about the following three points:

1. Let's start with the simplest example: var myobject = new object (); there is also an equivalent method: var myobject = {}; regardless of the method in which the final myobject variable is an instance of the object, its existence as an object has no other function.

2. During the instantiation process, the new keyword is used to notify js to create a new object. The new instance is assigned a variable so that you can reference the variable to access the new instance of the object. However, to do this, the instantiated object must be a constructor.

3. Each core object, such as object, function, array, and string, contains constructors.

After the object is instantiated, you cannot use the new keyword to create an exceptional instance based on the new instance.

VaR anotherobj = new myobject ();

An error is reported, indicating that myobject is not a constructor: Let's take a look:

<Title> Create your own object </title> 

Now we have created a myobject object, so we will create it:

<Title> Create your own object </title> 

An error is reported.

How to Create constructors:

1. function is the starting point for creating constructors.

<SCRIPT type = "text/JavaScript"> function myconstructor (a) {/* some code */} </SCRIPT>

2. The above code is equivalent to the following code:

<SCRIPT type = "text/JavaScript"> var myconstructor = new function ('A', '/* some code */'); </SCRIPT>

3. The following new method is not recommended because of performance problems.

4. The special feature of the function object is that its instance can also be used as a constructor, so it can be used to create a new instance of the function. Therefore, you can write the following code:

<SCRIPT type = "text/JavaScript"> function myconstructor (a) {/* some code */} var myobject = new myconstructor (); </SCRIPT>

In this case, the myconstructor function can be used as the constructor. When an object is added to an instance, the constructor executes the contained code.

<SCRIPT type = "text/JavaScript"> var myobject = {}; // new object (); function myconstructor (Message) {/* some code */Alert (Message ); this. mymessage = message;} var myobj = new myconstructor ('helo World'); alert (""); </SCRIPT>

The result is as follows:

In this example: Remember 2 points:

1. This references the myobj instance. by assigning the message parameter to this. mymessage, myobj has an attribute named mymessage that can be accessed at any time. VaR message = myobj. mymessage;

2. The mymessage attribute is only available in the instance of the instantiated myconstructor, And the myconstructor function itself is invalid:

function myConstructor(message){this.myMessage="abc";}alert(myConstructor.myMessage);

If the task runs successfully, ABC is displayed. Right. But the result is:

This illustrates our conclusion.

Add static method:

<SCRIPT type = "text/JavaScript"> var myobject = new object (); // Add an attribute myobject. name = 'object'; // Add a method named myobject. alertname = function () {alert (this. name);} // execute the add method myobject. alertname (); </SCRIPT>

The token is printed.

In the above Code, we add the name attribute and alertname () method as static members to the object instance directly on the myobject object through the dot operator. This is the most likely cause of misunderstanding. Because the static members only exist in the specific instances of the object and do not exist in the constructor.

If you start with a function object, the same problem also exists, but this object instance is also a constructor.

var myConstructor=function(){}myConstructor.name='jh';myConstructor.alertName=function(){alert(this.name);}myConstructor.alertName();

The code can also run normally, because myconstructor is both an instance and a constructor, but the name and alertname members cannot be applied to any new instances. If you create an instance of myconstructor, the following code is returned:

<script type="text/javascript">var myConstructor=function(){}myConstructor.name='jh';myConstructor.alertName=function(){alert(this.name);}//myConstructor.alertName();var myObj=new myConstructor();myObj.alertName();</script>

An error occurs:

It is a static member. It only exists in a specific instance of the object and does not exist in the constructor.

For details, we usually say the two statements are the same:

var myConstructor=function(){ }function myConstructor(){}

However, there is a detailed problem. The first method must be defined before it can be called. If we call the method first, an error will be reported, but the second method does not.

TianAdd public method:

1. The methods contained in the instantiated object are called public methods. You need to modify the function prototype, that is, the prototype attribute.

2. The prototype attribute is used to define special members of the object's internal structure. If you modify prototype, the modification will be immediately applied to its derived objects and instances.

3. When you modify the prototype of an object, any object that inherits the object and all existing instances will immediately inherit the same changes. Depending on the usage, this feature is both powerful and may cause problems. Therefore, be careful when you modify the existing but not the prototype of your object.

<SCRIPT type = "text/JavaScript"> function myconstructor () {// some code this. mymessage = "ABC";} var myobj = new myconstructor (); alert (myobj. mymessage); myconstructor. prototype. myname = 'handler'; alert (myobj. myname); </SCRIPT>

This prints out ABC and signature.

To add a public method, you only need to use the dot operator to add a method to its prototype:

Add private and privileged members:

Private Members are the variables and functions defined in constructors.

For example, to add private methods and attributes to myconstructor, you only need:

Privileged method:

Unlike private methods, privileged methods can be publicly accessed and Private Members can be accessed. Privileged methods refer to the methods defined using the this keyword in the scope of constructors:

In the preceding method, myconstructor has the same appendtomessage () method as prototype. However, the method belongs to the constructor scope and can access private members.

Summary of public, private, privileged, and static:

1. Private and privileged members are inside the function, and they will be taken to every instance of the function, thus occupying a large amount of memory.

2. A public prototype member is part of an object's blueprint. It is applicable to instantiating each instance of the object using the new keyword.

3. Static members are only applicable to one special instance of the object.

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.