Function对象的call,apply方法
来源:互联网 发布:红包尾数软件 编辑:程序博客网 时间:2024/06/10 12:44
//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 0
- Function对象的call,apply方法
- 详细说说ActionScript中function的call()方法apply()
- js function call,apply,bind方法
- JavaScript之Function的apply与call
- javascript函数对象的内部方法call(), apply(), bind()
- 关于Js函数对象的apply()和call()方法
- as3.0 Function.apply()与Function.call()方法简介
- 函数的apply、call方法
- apply function and call function
- js的 function, javascript内置对象apply,call,this概念及之间的关系详解
- 详细说说ActionScript中function的call()方法和apply()方法
- 详细说说ActionScript中function的call()方法和apply()方法
- 详细说说ActionScript中function的call()方法和apply()方法
- ECMA5Script中的this对象 ; call、apply方法
- JavaScript的this, 以及function.call, function.apply
- javascript之function对象包括call()和apply()
- 谈谈javascript的Function中那些隐藏的属性/方法:caller/callee/apply/call/bind
- MicrosoftAjax.Function.js 中用call 和apply方法实现回调和委托的代码
- 第一章 Java基础
- Artificial Intelligence resources
- 关于ESP8266与网络调试助手的通信
- Python:抓取网页初体验
- 关闭WIN7自动升级到WIN10及更高的系统版本
- Function对象的call,apply方法
- 公司早会演讲整理稿
- 蓝桥杯入门训练序列求和
- 面临的越多越迷茫,怎么破?
- 读书
- 顺序栈的实现
- WINXP控制面板没有切换经典视图解决方法
- python 廖雪峰之单元测试 + 源码
- 微信小程序的优势及前景