• JavaScript测试代码


    <!-- 在谷歌浏览器上的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] + "
    ";
    }
    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);
  • 相关阅读:
    json_encode 中文处理
    PHP 函数的参数
    IT菜鸟之OSI七层模型
    IT菜鸟之网线制作
    IT菜鸟之网站搭建(emlog)
    IT菜鸟之BIOS和VT
    IT菜鸟之虚拟机VMware的使用
    IT菜鸟之虚拟机VMware的安装
    IT菜鸟之计算机软件
    IT菜鸟之计算机硬件
  • 原文地址:https://www.cnblogs.com/wuyongyu/p/5680332.html
Copyright © 2020-2023  润新知