码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript测试代码

时间:2016-07-28 16:25:12      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

<!-- 在谷歌浏览器上的console运行 -->

 

//变量 

var netPrice = 8.99;
alert(netPrice);

 

//字符串方法 

var string1 = "我是字符串的第一部分+";
var string2 = "我是字符串的第二部分";
var longString = string1.concat(string2);
alert(longString);

var string1 = "The quick brown cat";
string1.indexOf("cat");
 //string1.indexOf("dog");

 

//继承 

function Pet() { 
    this.animal = "";
    this.name = "";
    this.setAnimal = function (newAnimal) {
        this.animal = newAnimal; 
    }
    this.setName = function (newName) {
        this.name = newName;
    }
}
var myCat = new Pet();
myCat.setAnimal("cat");
myCat.setName("miao");
alert(myCat.animal + " is call " + myCat.name)

function Dog () {
    this.breed = "";
    this.setBreed = function (newBreed) {
        this.breed = newBreed;
    }
}
Dog.prototype = new Pet();
var myDog = new Dog();
myDog.setName("hou");
myDog.setBreed("dog");
alert(myDog.name + " is a " + myDog.breed);

 

//用if功能检测 

if (document.getElementById) {
  myElement = document.getElementById(‘id‘);
  alert(myElement);
}else{
  alert("getElementById不可以用");
};

 

//测试json例子 

var jsonObject = {
    "param1" : "value1",
    "param2" : "value2",
    "param3" : "value3"
}
alert(jsonObject.param1);

 

//使用eval() 

var user = ‘{"username" : "miniwyy","location" : "China","height" : 1.70}‘;
var myObject = eval(‘(‘ + user + ‘)‘);
alert(myObject.username);

 

//向页面输出3 

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

 

//JSON.parse()

var Yori = ‘{"height" : 1.7,"age" : 23,"eyeColor" : "brown"}‘;
var myObject = JSON.parse(Yori);
var out = "";
for(i in myObject){
    out += i + " = " + myObject[i] + "\n";
}
alert(out);

 

//JSON的数据序列化 

var Yori = new Object();
Yori.height = 1.70;
Yori.age = 23;
Yori.eyeColor = "borwn";
alert(JSON.stringify(Yori));

 

//模拟关联数组 

var confence = {
    "startDay" : "Monday",
    "nextDay" : "TuesDay",
    "endDay" : "Wednesday"
}
alert(confence["startDay"]);

 

//利用匿名函数可以给对象添加方法 

var user ={
    "username" : "miniwyy",
    "location" : "China",
    "height" : 1.70,
    "setName" : function (newName) {
        this.username = newName;
    }
}
alert(user.username);
var newname = prompt("输入一个新的名字");
user.setName(newname);
alert(user.username);

 

//属性值可以是数组 

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

 

//JSON对象 

var bookListObject = {
    "booklist" :[
        {"title" : "Foundation", "author" : "Isaac Asimov"},
        {"title" : "Dune", "author" : "Frank Herbert"},
        {"title" : "Eon", "author" : "Greg Bear"},
        {"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测试代码

标签:

原文地址:http://www.cnblogs.com/wuyongyu/p/5680332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!