Function对象的call,apply方法

//call,apply from MDN

        //1 using call to chain constructors for an object
        function Product(name,price){
            this.name = name;
            this.price = price;
            if(price < 0){
                throw new RangeError("can't create product " + this.name + " with a negative price");
            }
        }

        function Food(name,price){
            Product.call(this,name,price);
            //alert(this.name);
            this.category = 'food';
        }

        function Toy(name,price){
            Product.call(this,name,price);
            //alert(this.name);
            this.category='toy';
        }

        var cheese = new Food('feta',5);
        var fun = new Toy('robot',40);

        
        //2 using call to invoke an anonymous function
        var animals=[{
            species:'Lion',name:'King'
        },{
            species:'Whale',name:'Fail'
        }];

        for(var i=0; i < animals.length;i++){
            (function(i){
                this.print = function(){
                   console.log('#'+i+" " + this.species + ":" + this.name);
                };
                this.print();
            }).call(animals[i],i);
        }

       
        //3 using call to invoke a function and specifying the context for 'this'
        function greet(){
            var reply = [this.person,'is an awesome',this.role].join(' ');
            alert(reply);
        }

        var i = {
            person:'Douglas Crockford',
            role:'Javascript Developer'
        };
        greet.call(i);

        /*
         thisArg
         The value of this provided for the call to fun. Note that this may not be the actual value seen by the method:
         if the method is a function in non-strict mode code, null and undefined will be replaced with the global object
         and primitive values will be converted to objects.
         */

//apply from MDN
         //using apply to chain constructors
         /*Function.prototype.construct=function(aArgs){
             alert(this);
             alert(this.prototype);
             var oNew = Object.create(this.prototype);
             this.apply(oNew,aArgs);
             return oNew;
         };*/

         Function.prototype.construct=function(aArgs){
             var fConstructor = this;
             var fNewConstr=function(){
                 fConstructor.apply(this,aArgs);
             };
             fNewConstr.prototype = fConstructor.prototype;
             return new fNewConstr();
         };

         function MyConstructor(){
             for(var nProp = 0; nProp < arguments.length;nProp++){
                 this['property'+nProp]=arguments[nProp];
             }
         }

         var myArray = [4,'Hello world!',false];
         var myInstance = MyConstructor.construct(myArray);

         alert(myInstance.property1);
         alert(myInstance instanceof  MyConstructor);
         alert(myInstance.constructor);

         //using apply and built-in functions
         var numbers = [5,6,2,3,7];
         var max = Math.max.apply(null,numbers);
         var min = Math.min.apply(null,numbers);
         alert(max);
         alert(min);

        //using apply in "monkey-patching"
        var originalfoo = someobject.foo;
        someobject.foo = function() {
            // Do stuff before calling function
            console.log(arguments);
            // Call the function as it would have been called normally:
            originalfoo.apply(this, arguments);
            // Run stuff after, here.
        }

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值