Understanding of Javascript Object literal, javascript Object literal

Source: Internet
Author: User

Understanding of Javascript Object literal, javascript Object literal

Output Method and definition benefit of object literal volume

1. There are two ways to output the object literal: traditional '. ', And the array method,When output in array mode, square brackets must be enclosed in quotation marks, as shown in figure

var box = {  name:'abc';  age:28};alert(box['name']);

Define methods for objects,

A: IfTraditional definition objectMethod,You need to define the method first, and then assign this method name to an attribute of the object. If you want to call this method without parentheses, it is to return the method code; if you want to call this method, add brackets to the object property to obtain the return value of the method.

Function objrun () {return '000000';} var box = new Object (); box. name = 'abc'; box. age = 28; box. run = objrun; alert (box. run (); // Result 123 // alert (box. run); // The result is function objrun () {return '000000';} // If the box. run = objrun (); // alert (box. run); // The result is 123. If the parentheses exist, an error is returned.

B:Defined literally,You only need to write the function directly on this attribute of the object. There is no function name on this function. It is an anonymous function.How can we call this method? Use the property name of the object to call the method, just like above.

For example:

var box = {  name:'abc',  age:28,  run:function(){    return '123';  }}alert(box.run());

2. Define the object literal volume to easily handle the situations where a large number of function parameters need to be output one by one. His countermeasure isInput an object to the function, which is defined literally. The relationship between attributes and values can be clearly indicated.Because the function is only a piece of code and must be called before execution.

For example:

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"> 

The above is all about the content of this article. For more information about the JavaScript syntax, see: JavaScript reference tutorial and JavaScript code style guide. I also hope you can provide more support.

Related Article

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.