How to output object literals and the benefits of definition
1. There are two ways to output object literals: traditional. ', and array method, but when outputting in array method, square brackets should be enclosed in quotes, such as
var box = { name:'abc'; age:28};alert(box['name']);Define methods for objects,
A: If you use the traditional way of defining an object , 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 brackets, you will return the method code; if you want to call this method and add brackets to the object attribute, you will get the return value of the method.
function objrun(){ return '123';}var box = new Object();box.name='abc';box.age = 28;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. If brackets are included, an error will be reportedB: Define it with a literal value . Just write a function directly on the object's property. There is no function name on this function. It is an anonymous function . So how do you call this method? Use the object's property name and call the method, just the same as above.
like:
var box = { name:'abc', age:28, run:function(){ return '123'; }}alert(box.run());2. The definition of object literals can easily solve the situation where a large number of functions parameters need to be output one by one. His countermeasure is to pass an object to the function, and this object is defined in a literal way. The corresponding methods of attributes and values can be clearly understood at a glance , because the function is just a piece of code and must be called to execute
like:
function AA(obj){ alert(obj.name); alert(obj.age);}var obj = { name: 'abc', age: 28}AA(obj);js object literal demo
/** * @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"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Literal--Literal</title> <script type="text/javascript" src="jslib/literal.js"></script> </head> <body> <input type="button" value="add" onclick="javascript:literal.add()"/> <input type="button" value="delete" onclick="javascript:literal.del()"/> <input type="button" value="update" onclick="literal.update()"/> <input type="button" value="name" onclick="javascript:alert(literal.name)"/> <input type="button" value="update" onclick="literal.update()"/> <input type="button" value="name" onclick="javascript:alert(literal.name)"/> <input type="button" value="name" onclick='javascript:alert(literal["name"])'"/> <input type="button" value="caller" onclick='javascript:literal.callLiteral()'"/> </body></html>
The above is the entire content of this article. For more information about JavaScript, you can check out: "JavaScript Reference Tutorial" and "JavaScript Code Style Guide". I also hope that everyone will support Wulin.com more.