Created
December 23, 2013 05:56
-
-
Save zhangwc/8092270 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| apply方法能劫持另外一个对象的属性和方法,从而实现继承。 | |
| Func.apply(obj, args); | |
| obj: 代替Func类里的this对象 | |
| args: 数组,作为参数 | |
| 例如: | |
| function Person(name,age) | |
| { | |
| this.name=name; | |
| this.age=age; | |
| alert("name:"+name+" age:"+age+" grade:"+arguments[2]); | |
| } | |
| function Student(name,age,grade) | |
| { | |
| Person.apply(this,arguments); | |
| this.grade=grade; | |
| } | |
| var student=new Student("qian",21,"一年级"); | |
| 通俗一点讲就是:用student去执行Person这个类里面的内容,在Person这个类里面存在this.name等之类的语句,这样就将属性创建到了student对象里面。 | |
| apply的妙用 | |
| 1)求最大、小值 | |
| var max = Math.max.apply(null,array); // 向max方法传递arguments参数序列 | |
| 2)合并数组 | |
| var arr1=new Array("1","2","3"); | |
| var arr2=new Array("4","5","6"); | |
| Array.prototype.push.apply(arr1,arr2); | |
| func.Call(obj, arg1, arg2, ...) 类似 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment