How object literals are exported and what benefits are defined
1. There are two types of output for object literals: traditional '. ', as well as array mode, only when output in an array, the square brackets should be enclosed in quotation marks, such as
var box = {
name: ' abc ';
Age:28
};
Alert (box[' name '));
Defines a method for an object,
A: If you define objects in a traditional way, you need to define the method first and then assign the method name to an attribute of the object, if you want to call this method without parentheses, return the method code, and if you want to call this method the object property is followed by parentheses, and the return value of the method is obtained
function Objrun () {return
' 123 ';
}
var box = new Object ();
Box.name= ' abc ';
Box.age =;
Box.run = Objrun;
Alert (Box.run ()); The result is 123
//alert (box.run); The result is function Objrun () {return ' 123 ';}
If Box.run = Objrun ();
alert (box.run); The result is 123, and if you bring the parentheses, you're going to get an error.
B: The literal definition , just to directly in the object of this property, write function on the line, this function has no function name, he is an anonymous function , then how to call this method, with the object of this property name, to call the method , ditto on the line
Such as:
var box = {
name: ' abc ',
age:28,
run:function () {return
' 123 ';
}
}
Alert (Box.run ());
2. The definition of the literal amount of the object, you can easily handle the function of a large number of parameters need one by one corresponding output. His response is to pass in an object to the function, which is defined in a literal way, and the way in which attributes and values correspond can be at a glance, because the function is only a piece of code and must be invoked to perform
Such as:
function AA (obj) {
alert (obj.name);
alert (obj.age);
}
var obj = {
name: ' abc ',
age:28
}
AA (obj);
The demo of JS object literal quantity
/**
* @author Zhanghua
/var literal = {
add:function () {
alert ("Add");
},
del: function () {
alert (' delete ');
},
update:function () {
alert ("Update");
},
name: " Zhangsan ",
callliteral:function () {
///For calls to the current literal object, add this keyword
this.add ();
}
;
HTML file:
<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
This is the entire article, learn more about JavaScript syntax, you can see: "JavaScript Reference Tutorial," JavaScript Code style guide, and also hope that you support the cloud-Habitat community.