Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

第 56 期(W3C 标准-ECMAScript-上下文环境):模拟apply方法 #59

Copy link
Copy link
@wingmeng

Description

@wingmeng
Issue body actions

ECMAScript 中的 apply() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数。
下面我们编写一个函数 myapply 来模拟 apply 方法,以此加深对 apply 的理解。

相关:第 52 期(W3C 标准-ECMAScript-上下文环境):模拟call方法

Function.prototype.myApply = function(context, arr) {
  context = context || window;
  context.fn = this;

  var result;

  if (!arr) {
    result = context.fn();
  } else {
    var args = [];

    for (var i = 0; i < arr.length; i++) {
      args.push('arr[' + i + ']');
    }

    result = eval('context.fn(' + args + ')');
  }

  delete context.fn;
  return result;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.