面向对象的JavaScript-006-Function.prototype.apply()的3种作用
1.
// Function.prototype.apply()的作用
// 1.Using apply to chain constructors
Function.prototype.construct = function (aArgs) {
var oNew = Object.create(this.prototype);
this.apply(oNew, aArgs);
return oNew;
};
// 或者The Object.create() method used above is relatively new. For an alternative method using closures, please consider the following alternative:
Function.prototype.construct = function(aArgs) {
var fConstructor = this, 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); console.log(myInstance);
console.log(myInstance.property1); // logs 'Hello world!'
console.log(myInstance instanceof MyConstructor); // logs 'true'
console.log(myInstance.constructor); // 2.Using apply and built-in functions
// min/max number in an array
var numbers = [5, 6, 2, 3, 7]; // using Math.min/Math.max apply
var max = Math.max.apply(null, numbers);
// This about equal to Math.max(numbers[0], ...)
// or Math.max(5, 6, ...) var min = Math.min.apply(null, numbers);
console.log(max);
console.log(min);
// vs. simple loop based algorithm
max = -Infinity, min = +Infinity; for (var i = 0; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
if (numbers[i] < min) {
min = numbers[i];
}
}
console.log(max);
console.log(min); function minOfArray(arr) {
var min = Infinity;
var QUANTUM = 32768; for (var i = 0, len = arr.length; i < len; i += QUANTUM) {
var submin = Math.min.apply(null, arr.slice(i, Math.min(i+QUANTUM, len)));
min = Math.min(submin, min);
} return min;
} var min = minOfArray([5, 6, 2, 3, 7]);
console.log(min); // 3.Using apply in "monkey-patching"
// Apply can be the best way to monkey-patch a built-in function of Firefox, or JS libraries. Given someobject.foo function, you can modify the function in a somewhat hacky way, like so:
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.
}
面向对象的JavaScript-006-Function.prototype.apply()的3种作用的更多相关文章
- javascript中 Function.prototype.apply()与Function.prototype.call() 对比详解
Function.prototype.apply()|Function.prototype.call() apply()方法可以在使用一个指定的 this 值和一个参数数组(或类数组对象)的前提下调用 ...
- 关于Function.prototype.apply.call的一些补充
宿主对象,在javascript中有三类对象,本地对象,内置对象和宿主对象.其他两类暂且不提,宿主对象是指什么呢(DOM BOM),控制台对象是文档对象模型的扩展,也被认为是宿主对象.那么,它们有什么 ...
- 探索 Reflect.apply 与 Function.prototype.apply 的区别
探索 Reflect.apply 与 Function.prototype.apply 的区别 众所周知, ES6 新增了一个全局.内建.不可构造的 Reflect 对象,并提供了其下一系列可被拦截的 ...
- Function.prototype.apply.call
我们先从一道简单的题目开始,前几天在git上看到的: 定义log方法,它可以代理console.log的方法.log(1,2,3) => 1 2 3 通常,你的答案会是这样的: functi ...
- Function.prototype.apply.call 理解分析
首先需要了解apply,call的基本用法,其目的是改变调用方法中的this指向,将其指向为传入的对象,改变this的指向,两种方法接收参数的方式不同. 代码:console.log var cons ...
- 面向对象的JavaScript-007-Function.prototype.bind() 的4种作用
1. // Function.prototype.bind() 的作用 // 1.Creating a bound function this.x = 9; var module = { x: 81, ...
- 面向对象的JavaScript-005-Function.prototype.call()的3种作用
1. // call的3种作用 // 1.Using call to chain constructors for an object function Product(name, price) { ...
- Function.prototype.call 和 Function.prototype.apply 的区别
call和apply函数是function函数的基本属性,都可以用于更改函数对象和传递参数,是前端工程师常用的函数.具体使用方法请参考以下案列: 例如: 申明函数: var fn = function ...
- Function.prototype.apply()
文章地址:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply ...
随机推荐
- NetCore下模拟和使用Modbus工业通信协议
Tips: 1.目前NetCore下与Modbus通信的框架主要选择了 Modbus.Net https://github.com/parallelbgls/Modbus.Net 2.modbus是 ...
- JVM内存管理之垃圾搜集器精解(让你在垃圾搜集器的世界里耍的游刃有余)
引言 在上一章我们已经探讨过hotspot上垃圾搜集器的实现,一共有六种实现六种组合.本次LZ与各位一起探讨下这六种搜集器各自的威力以及组合的威力如何. 为了方便各位的观看与对比,LZ决定采用当初写设 ...
- JAVA课程设计(坦克大战)
2019-01-16 坦克大战游戏背景: 1. 需求分析 1.1环境要求 操作系统:Windows 7(SP1)以上 JAVA虚拟机:JDK1.8以上 开发环境:Eclipse(4.5以上) 1.2角 ...
- centos7 桥接配置
cd /etc/sysconfig/network-scripts/ 名字可能各不同,一般出现在第一个位置 vim ifcfg-ens33 然后重启 systemctl restart network ...
- [Java][Web]Request 获取请求头和数据
获取方式一 InputStream in = request.getInputStream(); int len = 0; byte buffer[] = new byte[1024]; while( ...
- node的close
在http.ServerResponse对象的end方法被调用之前,如果连接被中断,将触发http.ServerResponse对象的close事件. var http=require("h ...
- c++ 双向链表操作总结
第一.包含DoubleLinkNode 模板类和DoubleLinkList 模板类 #pragma once #include<iostream> using namespace std ...
- Ceph添加/删除Mon(ceph.conf)
操作环境 ceph 0.87.7 Openstack liberty ubuntu 14.04 当前ceph配置文件如下 [global]fsid = c010eb34-ccc6-458d-9a03- ...
- Python 小结
1. Python pass是空语句,是为了保持程序结构的完整性. pass 不做任何事情,一般用做占位语句. 2.删除一个list里面的重复元素 方法一:是利用map的fromkeys来自动过滤重复 ...
- interrupt 1 using 1
释疑:void Timer0() interrupt 1 using 1 Timer0 是函数名,随便取的 interrupt xx using y 跟在interrupt 后面的 ...