Summary of several methods for declaring objects in JavaScript

Source: Internet
Author: User

1. var myObject = {};
2. function myObject ()
{
....
}
3. var myObject = function (){};
For the last two methods, we can also add parameters, which is similar to a constructor with parameters.
For example:
Function myObject (msg)
{
Alert (msg );
}
Var newObject = new myObject ('hello, World! ');


Var myObject = function (msg)
{
Alert (msg + 'again ');
}
Var newTwoObject = new myObject ('hello, World !) ;


We can even use strings to declare functions, which makes our programs more flexible.
For example:
Var myObject = new Function ("msg", "alert (msg )");
// Function can have multiple entry parameters. The last parameter is used as the method body.
Var newObject = new myObject ('Hell, World !) ;

JavaScript member declaration
In JavaScript, it is also very simple to declare a member of an object, but it is still slightly different from other advanced programs.
For example:
Var myObject = {
"FirstName": "thtwin ",
"LastName": "thtwinj2ee ",
"Age": 22,
"ShowFullName": function ()
{
Alert (this. FirstName + ''+ this. LastName );
}

};
MyObject. showFullName ();

Another object-oriented feature in JavaScript is that we can use. And [] reference members like advanced programming languages.
For example:
Var dateTime = {
Now: new Date (),
Show: function (){
Alert (new Date ());
}
};
Alert (dateTime. now );
It is equivalent:
Alert (dateTime. now );

DataTime. show ();
It is equivalent:
DateTime ["show ()"];

For method calls, in JavaScript, the base class of all objects is Object, and the base class defines many members through prototype.
And methods, such as toString and toLocaleString.
For example:
Var obj = {"toString": function () {return "This is an test! ";}};
Alert (obj );

At runtime, The toString () method is called when alert is running. In fact, when JavaScript needs to convert an object to a character
The toString () method of this object is called implicitly.
For example:
Date. prototype. toString = function () {alert ('this is a test! ');};
Var da = new Date ());

Date. prototype. toString = function () {alert ('this is a test! ');};
Var dt = new Date () + 1;

Use of the call method in JavaScript:
Call explanation:
The call method can be used to call a method instead of another object.
The call method can change the object context of a function from the initial context to the new object specified by thisObj.

For example:
Function abc ()
{
Alert (this. member1 );
}
Var obj = {member1: "Hello world! ", Show: abc };
Var obj2 = {member1: "Hello world again! ", Show: abc };

Obj. show ();
// You can also use
Abc. call (obj );
Abc. call (obj2 );

Another version after modification:
Member1 = 'test ';
Function abc ()
{
Alert (this. member1 );
}
Var obj = {member1: "Hello world", show: abc };
Var obj2 = {member1: "Hello world again", show: abc };

Obj. show ();
// You can also use
Abc. call (obj );
Abc. call (obj2 );

Abc (); // this in abc points to the current context
Each function has a call method. In the above process, we can see that another object is used to call the display method,
And noticed this changes in the object context.

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.