JavaScript uses abbreviated syntax to create an object's code _js object-oriented

Source: Internet
Author: User

<ptml> <pead> <title>javascript Create objects with abbreviated syntax </title> <script type= "Text/javascript" > Win Dow.onload = function () {appendmemberstoobject (); Simplesyntaxtocreateobject (); function Appendmemberstoobject () {var testname = ' Appendmemberstoobject '; Writestart (testname); Create an object, var person = new Object (); Adds an attribute (public) person to an object. Name = "Lipei"; Person. Sex = "Boy"; Person. Age = "24"; Add method (public) to an object person.tostring = function () {//here with person or this, at this point, this refers to the person object. return str = ' Name: ' + this. Name + ", Sex:" + this. Sex + ', Age: ' + this. Age; } WriteLine ("person.") Name: "+ person." Name); WriteLine ("person.tostring ():" + person.tostring ()); Writeend (); /* According to the results: in JavaScript, you can add members (properties, methods, and so on) to an object after the object is created. */} function Simplesyntaxtocreateobject () {var testname = "Simplesyntaxtocreateobject"; Writestart (testname); Using abbreviated syntax to create an object var computer = {Cpu: "Core 2P Series", mainboard: "Asus ER", keyboard: "Threesome", tostring:function () { return str = "CPU:" + this. Cpu + ", mainboard:" + this. Mainboard + ", keyboard:" + this. keyboard; } }; WriteLine ("Computer.") Mainboard: "+ computer." mainboard); WriteLine ("computer.tostring ():" + computer.tostring ()); Writeend (testname); /* According to the results: the JavaScript shorthand syntax can only recognize the type after the variable colon, and define the type of the variable accordingly. */}/*************************test tools**************************/function WriteLine (str) {document.write (str + "<br>"); function Writestart (str) {WriteLine ("TEST:" + str + "<br>"); function Writeend () {WriteLine ("<br><pr/>"); } </script> </pead> <body> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.