JavaScript test Code

Source: Internet
Author: User

<!-- -

Variable

var netprice = 8.99; alert (netprice);

String method

var string1 = "I am the first part of the string +"; var string2 = "I am the second part of the string"; var longstring = string1.concat (string2); alert (longstring); var string1 = "The quick brown Cat"; String1.indexof ("cat");//string1.indexof ("dog") ;

Inherited

functionPet () { This. Animal = "";  This. Name = "";  This. Setanimal =function(newanimal) { This. Animal =Newanimal; }     This. SetName =function(newName) { This. Name =NewName; }}varMycat =NewPet (); Mycat.setanimal ("Cat"); Mycat.setname ("Miao"); alert (Mycat.animal+ "is call" +mycat.name)functionDog () { This. Breed = "";  This. Setbreed =function(newbreed) { This. Breed =Newbreed; }}dog.prototype=NewPet ();varMydog =NewDog (); Mydog.setname ("Hou"); Mydog.setbreed ("Dog"); alert (Mydog.name+ "is a" + mydog.breed);

Detect with If function

If= document.getElementById (' id '); alert (myelement);} Else {alert ("getElementById cannot be used");};

Test JSON example

var jsonobject = {    "param1": "Value1",    "param2": "value2",    " Param3 ":" Value3 "}alert (jsonobject.param1);

Use eval ()

var user = ' {' username ': ' miniwyy ', ' Location ': ' China ', ' Height ': 1.70} '; var myObject = eval (' (' + user + ') '); alert (myobject.username) ;

Output to Page 3

var x = eval (4); Eval ("A=1;b=2;document.write (a+b);");

Json.parse ()

var Yori = ' {' height ': 1.7, ' age ': $, ' eyecolor ': ' Brown '} '; var myObject = json.parse (yori); var out = "";  for inch myObject) {    + = i + "=" + myobject[i] + "\ n";} alert (out);

Data serialization of JSON

var New  = 1.70= "Borwn"; alert (json.stringify (Yori));

Simulating associative arrays

var confence = {    "startday": "Monday",    "NextDay": "Tuesday",    " Endday ":" Wednesday "}alert (confence[" StartDay "]);

Use anonymous functions to add methods to objects

var user ={    "username": "Miniwyy",    "location": "China",    "height": 1.70,    function  (newName) {        this. Username= newName;    }} alert (user.username); var newname = Prompt ("Enter a new name"); User.setname (newname); alert (user.username) ;

The property value can be an array

var booklistobject = {    "Booklist": ["Foundation", "Dune", "Eon", "2001 A Space Odyssey", "Stranger In A Strange land "]}var book = booklistobject.booklist[2];alert (book);

JSON object

var booklistobject = {    "Booklist" : [        {"title": "Foundation", "Author": "Isaac Asimov "},        {" title ":" Dune "," Author ":" Frank Herbert "},        {" title ":" Eon "," Author ":" Greg Bea  R "},        {" title ":" 2001 A Space Odyssey "," Author ":" Arthur C. Clarke "},        {" title ":" Stranger In a Strange land "," Author ":" Robert A. Heinlein "}    ]}alert (booklistobject.booklist[2].author);

JavaScript test Code

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.