Secret of the JavaScript Ninja 学习笔记 - 4

第三章 Functions are fundamental

3.3 Invocations

The manner in which a function is invoked has a huge impact on how the code within it operates, primarily in how the this parameter is established.
There are four ways to invoke a function:
  1. As a function, in which the function is invoked in a straightforward manner.
  2. As a method, which ties the invocation to an object, enabling OOP.
  3. As a constructor, in which a new object is brought into being.
  4. Via its apply() or call() methods.

3.3.1 From arguments to function parameters

When a list of arguments is supplied as part of a function invocation, these arguments are assigned to the parameters specified in the function declaration in the same order that each was specified.
If there is a different number of arguments than parameters:
  1. If more arguments are supplied than parameters, the "excess" arguments are simply assigned to parameter names. But we still have a way to get at them.
  2. If there are more parameters than arguments, the parameters that have no corresponding argument are set to undefined.
THE ARGUMENTS PARAMETER
The arguments parameter is a collection of all of the arguments passed to the function. It has a length property and the individual argument values can be obtained using array indexing notation such as arguments[2]. But arguments IS NOT a JavaScript array.
THE "THIS" PARAMETER
Whenever a function is invoked, an implicit parameter named this is passed tho the function. What the this parameter points to isn't defined by how the function is declared, but by how it's invoked.

3.3.2 Invocation as a function

function ninjia(){};
ninjia();

function samurai = function() {};
samurai();

When invoked in this manner, the function context is the global context - the window object.

3.3.3 Invocation as a method

When a function is assigned to a property of an object and the invocation occurs by referencing the function using that property, then the function is invoked as a method of that object.
var o = {};
o.whatever = function(){};
o.whatever();

The object becomes the function context and is available within the function via the this parameter.

3.3.4 Invocation as a constructor

function creep() { return this; }
new creep();

THE SUPERPOWERS OF CONSTRUCTORS
When a constructor is invoked, the following actions take place:
  1. A new empty object is created
  2. This object is passed to the constructor as the this parameter, and thus becomes the constructor's function context
  3. In the absence of any explicit return value, the new object is returned as the constructor's value

3.3.5 Invocation with the apply() and call() methods

apply() and call() allow us to invoke a function and to explicitly specify any object we want as the function context.
function juggle() {
  var result = 0;
  for (var n = 0; n < arguments.length; n++) {
  result += arguments[n];
  }
  this.result = result;
}

var ninja = {};
juggle.apply(ninja, [1,2,3,4]);
juggle.call(ninja, 5, 6, 7, 8);

FORCING THE FUNCTION CONTEXT IN CALLBACKS
function forEach(list, callback) {
  for (var n = 0; n < list.length; n++) {
    callback.call(list[n], n);
  }
}

var weapons = ['shuriken', 'katana', 'nunchucks'];

forEach(weapons, function(index) {
  assert(this == weapons[index]);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值