js 中apply()与call()方法详解实例
Jun 01, 2016 am 09:54 AM
apply
apply()和call(), 它们的作用都是将函数绑定到另外一个对象上去运行,两者仅在定义参数方式有所区别:
apply( thisArg , argArray );
call( thisArg, [,arg1,arg2…] ] );
第一个参数表示所有函数内部的this指针都会被赋值为 thisArg,这可实现将函数作为另外一个对象的方法运行的目的.
<code><script type="text/javascript"> function sumFun(sum1,sum2){ return sum1+sum2; } function applySum(sum1,sum2){ //使用sumFun函数来完成一次调用,调用的参数就是applaySum这个函数的参数. 第二个参数表示一个数组。 return sumFun.apply(this,arguments); } alert(applaySum(12,45));//运行结果是57; function callSum(sum1,sum2){ return sumFun.call(this,sum1,sum2); } alert(callSum(12,45));//运行结果是57; </script></code>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial

8 Stunning jQuery Page Layout Plugins
