javascript语言精粹理解-笔记

阅读次数: 142

  • A+
所属分类:建站
javascript语言精粹理解-笔记

对象字面量

<script>
    var stooge = {
        "first-name":'jeoy',
        "last-name":'how'
    };
     if(typeof Object.prototype.beget !== 'function'){
        Object.beget = function (o){
            var F = function(){};
            F.prototype = o;
            return new F();
        }
    }
    var uget = function(o){
        var d = function(){};
        d.prototype = o;
        return new d();
    }

//    var another_stooge = Object.beget(stooge);
    var another_stooge = uget(stooge);
    another_stooge['first-name'] = 'cosmos';
    another_stooge['middle-name'] = 'space';
    another_stooge.nickname = 'jack';
    delete another_stooge['first-name'];
    var name;
    for (name in another_stooge){
        console.log(name,another_stooge[name]);
    }
</script>

核心点是

1. 新的对象会继承原型对象的值(当自己的新的对象中并没有声明和赋值的)

2.新的对象中某个属性删除时,如果原型对象中存在这个属性时则会对外显示原型属性

函数字面量

<script>
    var add = function(a,b){
        return a+b;
    };
    var myObject = {
        value:0,
        increment:function(inc){
            //方法调用模式
            this.value += typeof inc === 'number' ? inc : 1;
        }
    };
    myObject.increment();
    console.log(myObject.value);
    myObject.increment(3);
    console.log(myObject.value);
    myObject.double = function(){
        //函数调用模式
        var self = this;
        var helper = function(){
            self.value = add(self.value,self.value);
        }
        helper();
    };
    myObject.double();
    console.log(myObject.value);

    /* 构造器调用模式 start */
    var cosmos = function(str){
        this.status = str;
    }
    cosmos.prototype.get_status = function(){
        return this.status;
    }

    var mycosmos = new cosmos("jack");
    console.log(mycosmos.get_status());
    /* 构造器调用模式 end */

    /* apply 调用模式 start */
    var array = [3,4];
    var sum = add.apply(null,array);
    console.log(sum);

    var statusObject = {
        status:"A-OK"
    };
    var status = cosmos.prototype.get_status.apply(statusObject);
    console.log(status);
    /* apply 调用模式 end */
</script>

一共有四种

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: