js文件:
/** * @author zhanghua */ var literal = { add: function(){ alert("add"); }, del: function(){ alert("delete"); }, update: function(){ alert("update"); }, name: "zhangsan", callLiteral: function(){ // 对于当前字面量对象的调用,要加this关键字 this.add(); } };
<?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--字面量</title> <script type="text/javascript" src="jslib/literal.js"></script> </head> <body> <input type="button" value="add" οnclick="javascript:literal.add()"/> <input type="button" value="delete" οnclick="javascript:literal.del()"/> <input type="button" value="update" οnclick="literal.update()"/> <input type="button" value="name" οnclick="javascript:alert(literal.name)"/> <input type="button" value="name" οnclick='javascript:alert(literal["name"])'"/> <input type="button" value="caller" οnclick='javascript:literal.callLiteral()'"/> </body> </html>