[JavaScript-Function] Function Invocation/Call(函数调用) 以及call() and apply() 方法
介绍:JS函数中的代码会被函数被invoke(调用)时执行.
函数被定义时代码不执行,
函数调用时函数内的代码会被执行.
常用的term是 call a function 而不是 invoke a function.
function always belong to a object in javascript.
When a function does no tbelong to nay object. In javascript there is alaways a default global object.
在Html 中,是浏览器窗口本身. the global object will become a window function in this case.
That means it can be invoked by window.functionName().
1. this keyword : 代表当前代码的对象.
The value of this, when used in a function , is the objec that owns the function.
Note that this is not a variablke. It is a keyword. You cannot change the value of this.
2. Invoking a Function as a Method
In javascript you can defiune functions as object methods.(对象方法?),形如:
var myObject = {
firstName:"John",
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
}
}
myObject.fullName(); // Will return "John Doe"
3. 对于JSON对象,可以使用"."来调用对象内方法.
var myObject = {
firstName:"John",
lastName: "Doe",
fullName: function() {
return this.firstName+" "+this.lastName;
}
}
4.Invoking a Function with a Function Constructor

NOTE:
A constructor invocation creates a new object. The new object inherits the properties and methods from its constructor.
The this keyword in the constructor does not have a value.
The value of this will be the new object created when the function is invoked.
====================================Call==================================
All Functions are Methods:If a function is not a method of a JavaScript object, it is a function of the global object (see previous chapter).
1. The JavaScript call() method.

2. The call() method with Arguments.

Output:
John Doe,Oslo,Norway
=======================apply()=================
和call()方法类似.
区别是,call() 的参数是分隔开的, apply()的参数是一个数组.
person.fullName.call()变成了person.fullName.apply(person1,["Oslo","Norway"]);
====实例 Math.max(arg1,arg2,arg....)
Math.max(1,2,3);会返回3
但是JavaScript数组并没有max()方法,所以可以apply Math.max()方法.
Math.max.apply(null, [1,2,3]); // Will also return 3
第一个参数(null)无关紧要.在本例中不使用.
[JavaScript-Function] Function Invocation/Call(函数调用) 以及call() and apply() 方法的更多相关文章
- JavaScript学习笔记(1))——————call,apply方法
学习前端也有一段时间了,但是效果甚微.利用时间不够充分,虽然是利用工作之余来学习.但是这不能成为我的借口. 今天学习了(其实看了很多遍)call apply方法. function abc(a,b){ ...
- 全面理解Javascript中Function对象的属性和方法
http://www.cnblogs.com/liontone/p/3970420.html 函数是 JavaScript 中的基本数据类型,在函数这个对象上定义了一些属性和方法,下面我们逐一来介绍这 ...
- javascript 中function(){},new function(),new Function(),Function 摘录
函数是JavaScript中很重要的一个语言元素,并且提供了一个function关键字和内置对象Function,下面是其可能的用法和它们之间的关系. function使用方式 var foo01 = ...
- JavaScript中Function Declaration与Function Expression 或者说 function fn(){}和var fn=function(){} 的区别
JavaScript是一种解释型语言,函数声明会在JavaScript代码加载后.执行前被解释,而函数表达式只有在执行到这一行代码时才会被解释. 在JS中有两种定义函数的方式, 1是:var aaa= ...
- JavaScript中Function函数与Object对象的关系
函数对象和其他内部对象的关系 除了函数对象,还有很多内部对象,比如:Object.Array.Date.RegExp.Math.Error.这些名称实际上表示一个 类型,可以通过new操作符返回一个对 ...
- JavaScript笔记 Function
在JavaScript中方法由两部分组成: 方法名和方法体. JavaScript中的方法跟其他传统面向对象语言不同,它跟普通的变量没有区别,唯一不同点是它是Function对象,因此它会有一些Fun ...
- (转)深入理解javascript的function
原文:http://www.cnblogs.com/sharpxiajun/archive/2011/09/16/2179323.html javascript笔记:深入理解javascript的fu ...
- javascript的Function 和其 Arguments
http://shengren-wang.iteye.com/blog/1343256 javascript的Function属性:1.Arguments对象2.caller 对调用单前函数的Func ...
- JavaScript之Function函数深入总结
整理了JavaScript中函数Function的各种,感觉函数就是一大对象啊,各种知识点都能牵扯进来,不单单是 Function 这个本身原生的引用类型的各种用法,还包含执行环境,作用域,闭包,上下 ...
随机推荐
- Luogu 1309 - 瑞士轮 - [归并排序]
题目链接:https://www.luogu.org/problemnew/show/P1309 题解: 每次比赛前,每个人都是按照分数降序排好的,那么比赛完后,将选手按输赢分成两组,顺序依然按照原顺 ...
- 11.1-uC/OS-III就绪列表
准备好运行的任务被放到就绪列表中, 如图6-1.就绪列表是一个数组( OSRdyList[]),它一共有OS_CFG_PRIO_MAX条记录,记录的数据类型为OS_RDY_LIST(见OS.H).就绪 ...
- UIWebView 缓存
//存储cookie的方法 - (void)saveCookies { // 创建一个可变字典存放cookie NSMutableDictionary *fromappDict = [NSMutabl ...
- Golang--匿名变量
在使用多重赋值时,如果不需要在左值中接收变量,可以使用匿名变量(anonymous variable). 匿名变量的表现是一个下画线_,使用匿名变量时,只需要在变量声明的地方使用下画线替换即可.例如: ...
- Python Async/Await入门指南
转自:https://zhuanlan.zhihu.com/p/27258289 本文将会讲述Python 3.5之后出现的async/await的使用方法,以及它们的一些使用目的,如果错误,欢迎指正 ...
- JDK 1.8源码阅读 HashSet
一,前言 类实现Set接口,由哈希表支持(实际上是一个 HashMap集合).HashSet集合不能保证的迭代顺序与元素存储顺序相同.HashSet集合,采用哈希表结构存储数据,保证元素唯一性的方式依 ...
- 从零开始一起学习SLAM | 理解图优化,一步步带你看懂g2o代码
首发于公众号:计算机视觉life 旗下知识星球「从零开始学习SLAM」 这可能是最清晰讲解g2o代码框架的文章 理解图优化,一步步带你看懂g2o框架 小白:师兄师兄,最近我在看SLAM的优化算法,有种 ...
- Python压缩指定文件及文件夹为zip
Python压缩指定的文件及文件夹为.zip 代码: def zipDir(dirpath,outFullName): """ 压缩指定文件夹 :param dirpat ...
- Java作业(一)
再此次的Java考试中发现自己的不足,无法套用HTML模板进行编程,说实话拿到的第一刻自己有些懵,不知道怎么去操作, 后来通过查询操作了一些,但是整个人还是懵懵的,不知道下一步怎么办,怎么去连接后台和 ...
- Spring框架的第三天
## Spring框架的第三天 ## ---------- **课程回顾:Spring框架第二天** 1. IOC的注解方式 * @Value * @Resource(name="" ...