惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
腾讯CDC
Jina AI
Jina AI
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
小众软件
小众软件
M
MIT News - Artificial intelligence
MyScale Blog
MyScale Blog
D
Docker
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
月光博客
月光博客
L
LangChain Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
C
Check Point Blog
U
Unit 42
人人都是产品经理
人人都是产品经理

博客园 - oisiv

java线程池ThreadPoolTaskExecutor执行顺序 css一些特别效果设定 SQL模擬死結產生 使用jquery模拟键盘事件,但window系统并不会真的响应事件,只是浏览器当前页面会响应而已 base64编码的图片字节流存入html页面中的显示 设置span 宽度的完美解决方案 Mercurial hg web server的配置 DIV撑开 java.net.URLEncode编码 与 URLDecode解码问题 java在CMD环境下执行需注意字符集设定 把MSSQL的表数据查询成 insert into格式的函数 配置jboss4.2.3GA启用SSL html中DIv并排显示问题 鲁迅《兩地書》 java采用Apache FileUpload组件实现上传 可执行jar包的处理,以及could not find the main class win7/xp 双击执行jar包出现:could not find the main class的问题处理 MSSql性能优化 CSS拼装图片时,确保严丝合缝 在 SQL Server 2008 中新建用户登录并指定该用户的数据库
js中对象复制以及apply方法的使用
oisiv · 2013-04-01 · via 博客园 - oisiv

1、对象的继承,一般的做法是复制:Object.extend

prototype.js的实现方式是:

Object.extend = function(destination, source) { for (property in source) { destination[property] = source[property]; } return destination; }

除此之外,还有种方法,就是:Function.apply(当然使用Function.call也是可以的)

apply方法能劫持另外一个对象的方法,继承另外一个对象的属性

Function.apply(obj,args)方法能接收两个参数

obj:这个对象将代替Function类里this对象

args:这个是数组,它将作为参数传给Function(args-->arguments)

apply示范代码如下:

<script> function Person(name,age){ //定义一个类,人类 this.name=name; //名字 this.age=age; //年龄 this.sayhello=function(){alert("hello")}; } function Print(){ //显示类的属性 this.funcName="Print"; this.show=function(){ var msg=[]; for(var key in this){ if(typeof(this[key])!="function"){ msg.push([key,":",this[key]].join("")); } } alert(msg.join(" ")); }; } function Student(name,age,grade,school){ //学生类 Person.apply(this,arguments); Print.apply(this,arguments); this.grade=grade; //年级 this.school=school; //学校 } var p1=new Person("jake",10); p1.sayhello(); var s1=new Student("tom",13,6,"清华小学"); s1.show(); s1.sayhello(); alert(s1.funcName); </script>

学生类本来不具备任何方法,但是在Person.apply(this,arguments)后,

他就具备了Person类的sayhello方法和所有属性。

在Print.apply(this,arguments)后就自动得到了show()方法

2、利用Apply的参数数组化来提高

Function.apply()在提升程序性能方面的技巧

我们先从Math.max()函数说起,Math.max后面可以接任意个参数,最后返回所有参数中的最大值。

比如 alert(Math.max(5,8)) //8 alert(Math.max(5,7,9,3,1,6)) //9

但是在很多情况下,我们需要找出数组中最大的元素。 var arr=[5,7,9,1] alert(Math.max(arr)) // 这样却是不行的。一定要这样写

function getMax(arr){ var arrLen=arr.length; for(var i=0,ret=arr[0];i<arrLen;i++){ ret=Math.max(ret,arr[i]); } return ret; }

这样写麻烦而且低效。如果用 apply呢,看代码: function getMax2(arr){ return Math.max.apply(null,arr); } 两段代码达到了同样的目的,但是getMax2却优雅,高效,简洁得多。

再比如数组的push方法。 var arr1=[1,3,4]; var arr2=[3,4,5]; 如果我们要把 arr2展开,然后一个一个追加到arr1中去,最后让arr1=[1,3,4,3,4,5] arr1.push(arr2)显然是不行的。 因为这样做会得到[1,3,4,[3,4,5]]

我们只能用一个循环去一个一个的push(当然也可以用arr1.concat(arr2),但是concat方法并不改变arr1本身) var arrLen=arr2.length for(var i=0;i<arrLen;i++){ arr1.push(arr2[i]); }

自从有了Apply,事情就变得如此简单 Array.prototype.push.apply(arr1,arr2)