GithubHelp home page GithubHelp logo

js-fp's People

Contributors

idealv avatar

js-fp's Issues

call,apply,bind,new的模拟实现

本文是看完冴羽大大的博客后的笔记
使用一个指定的this值和若干个指定参数的情况下调用某个函数和某个方法
call的实现:改变this指向,指向绑定的对象,执行函数

`Function.prototype.call2 = function (context) {
context = context || window;
//this:绑定的函数
context.fn = this;
var args = [];
for (var i = 1; i < arguments.length; i++) {
//从1开始,也就是从要绑定的对象之后开始视作传入参数
args.push('arguments[' + i + ']');
}
var result = eval('context.fn(' + args + ')');
//为对象创建了一个方法,为方法传入参数执行完毕后,删除这个方法
delete context.fn;
return result;
};

var foo = {
a: 2
};

var bar = function () {
console.log(this.a);
};

bar.call2(foo);

//apply的实现,跟call的区别之处,传入参数为数组形式
Function.prototype.apply2 = function (context, arr) {
context = Object(context) || window;
context.fn = this;
var result;
//传入的不是对象,视作无对象直接执行函数
if (!arr) {
result = context.fn();
} else {
var args = [];
for (var i = 1, len = arguments.length; i < len; i++) {
args.push('arguments[' + i + ']');
}
result = eval('context.fn(' + args + ')');
}
delete context.fn;
return result;
};

Function.prototype.bind2 = function (context) {
if (typeof this !== "function") {
throw new Error("Function.prototype.bind - what is trying to be bound is not callable");
}
var self = this;
var args = [].slice.call(arguments, 1);
//中间对象fNop,这样继承构造函数的对象修改它的原型不会把self的原型一起修改
var fNop = function () { };
var fBound = function () {
var bindArgs = [].slice.call(arguments);
//若将返回的函数作为构造函数新建一个实例,则将this绑定到这个新建的对象上。如果作为普通函数,则正常传参
return self.apply(this instanceof fBound ? this : context, args.concat(bindArgs));
};
fNop.prototype = self.prototype;
fBound.prototype = new fNop();
return fBound;
};

function objectFactory() {
var obj = new Object(),
constructor = [].shift.call(arguments);
//指向构造函数的原型
obj.proto = constructor.prototype;
//将构造函数的this指向新建对象,这样对象可以访问构造函数上的属性
var ret = constructor.apply(obj, arguments);
//如果构造返回值是一个对象,则返回这个对象,如果不是对象,改返回什么就返回什么
return typeof ret === 'object' ? ret : obj;
}`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.