zoukankan      html  css  js  c++  java
  • JavaScript例子

    1.

    console.log("ddd")
    
    var data = [];
    for(var i = 0 ; i < 3; i++){
        data[i]=function() {
            console.log(i);
        }
    }
    
    data[0]();// 3
    data[1]();// 3
    data[2]();// 3
    console.log("-----------");
    function counter() {
        var x = 0;
        
        return {
            increase: function increase() { return ++x; },
            decrease: function decrease() { return --x; }
        };
    }
    
    var ctor = counter();
    
    console.log(ctor.increase());
    console.log(ctor.decrease());
    
    console.log(ctor.increase);
    console.log(ctor.decrease);
    
    
    console.log("-----------");
    
    let a = 1, b = {c: 3};
    a = 2;
    var s={c: a} = b;
    console.log(a);
    console.log("-----------");
    
    console.log(new Date("2021/10/15 15:00:12"));
    console.log(new Date("2021-10-15 15:00:13"));
    console.log("-----------");
    
    
    function func() {
        console.time("func");
        var obj = {
            a: 1
        };
        for(var i = 0; i < 100000; i++)
        {
            obj.a=i;
        }
        console.timeEnd("func");
    }
    func();
    
    function funcWith() {
        console.time("funcWith");
        var obj = {
            a: 1
        };
        with(obj) {
            for(var i = 0; i < 100000; i++) {
                a = i;
            }
        }
        console.timeEnd("funcWith");
    }
    
    funcWith();
    console.log("-----------");
    
    console.log(false.toString());
    console.log(false);
    
    console.log(5 + null)    // 返回 5         null 转换为 0
    console.log("5" + null)  // 返回"5null"   null 转换为 "null"
    console.log("5" + 1)     // 返回 "51"      1 转换为 "1" 
    console.log("5" - 1)     // 返回 4         "5" 转换为 5
    
    console.log("-----------");
    
    var y = "5";      // y 是一个字符串
    var x = 1 + y;  
    console.log(x)
    console.log("-----------");
    
    var x = 0.1;
    var y = 0.2;
    var z = x + y;
    if(z*10==30){
        console.log("yes")
    }
    console.log(z)
    console.log("-----------");
    
    websites = {site:"菜鸟教程", url:"www.runoob.com", like:460,}
    var colors = [5, 6, 7,]; //这样数组的长度可能为3 也可能为4。
    if (typeof myObj !== "undefined" && myObj !== null) 
    // if (myObj !== null && typeof myObj !== "undefined") 
    
        console.log(colors.length)
        else
            console.log(colors.length+1)
    console.log("-----------");
    
    var obj = {
        birth: 1990,
        getAge: function (year) {
            var b = this.birth; // 1990
            var fn = (y) => y - this.birth; // this.birth仍是1990
            return fn.call({birth:2000}, year);
        }
    };
    var obj1={
        birth:2005
    }
    console.log(obj.getAge(2015)); // 25 ,fn.call({birth:2000}, year)传递的对象无用
    console.log(obj.getAge.call(obj1,2015));//10 
    console.log("__________")
    
    
    var person1 = {
      firstName:"Johnrrr",
      lastName: "Doerrr",
      fullName: function() {
          var fn = () => this.firstName + " " + this.lastName;
        return fn.call();
      }
    //  fullName: () => {return this.firstName + " " + this.lastName;}
      
    }
    var person2 = {
      firstName:"John",
      lastName: "Doe",
    }
    console.log(person1.fullName()) //Johnrrr Doerrr
    console.log(person1.fullName.call(person2));  // 返回 "John Doe"
    
    console.log("__________")
    
    let tea = [1,2,{name:true}];
    let arr4 = tea.map((it,id)=>{
        if (id===2) {
            it.name = 'edit'
        }
         return it
    })
    
    console.log(tea[2].name)

  • 相关阅读:
    三分钟学会.NET微服务之Polly
    redis设置密码和redis主从复制
    程序员工作之外,如何再赚一份工资?
    吞吐量(TPS)、QPS、并发数、响应时间(RT)概念
    TPS和QPS的区别和理解
    制定一套适合自己团队的GITflow标准化工作流
    UvaLive 6600 Spanning trees in a secure lock pattern 矩阵行列式
    从零開始学Xamarin.Forms(一) 概述
    Unity 之 C# 利用回调函数实现C++匿名函数
    hdu 4324 Triangle LOVE(拓扑判环)
  • 原文地址:https://www.cnblogs.com/sunupo/p/15479453.html
Copyright © 2011-2022 走看看