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