Home > Web Front-end > JS Tutorial > Summary of several methods of declaring objects under JavaScript_javascript skills

Summary of several methods of declaring objects under JavaScript_javascript skills

WBOY
Release: 2016-05-16 19:00:09
Original
1146 people have browsed it

1.var myObject = {};
2.function myObject()
{
....
}
3.var myObject = function(){};
For For the latter 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!);


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

Declaration of members in JavaScript
In JavaScript, it is very simple to declare the members of an object, but unlike other advanced programs Still slightly different
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 a high-level programming language.
For example:
var dateTime = {
now : new Date(),
show : function(){
alert(new Date()) ;
}
} ;
alert(dateTime.now) ;
Equivalent to:
alert (dateTime.now) ;

dataTime.show() ;
is equivalent to:
dateTime["show()"] ;

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

When running, the toString() method is called when alert occurs. In fact, when JavaScript When an object needs to be converted into a character
, the object's toString() method is implicitly called.
For example:
Date.prototype.toString = function(){alert('this is a test! ') ;} ;
var da = new Date(new Date()) ;

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

Usage of call method in JavaScript:
Explanation about call:
The call method can be used to call a method instead of another object. The
call method changes 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 modified version:
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(); // At this time, this in abc points to the current context
Each function has a call method. In the above process, we see that another object is used instead of calling the display method,
and notice that this is in the object context changes.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template