Coder Social home page Coder Social logo

blog's People

Contributors

c-somnus avatar

Watchers

 avatar

blog's Issues

模拟实现call、apply、bind

var foo = {
a: 2,
fn: function() {
console.log(this.a)
}
}

foo.fn()
a

// call
// .call(context, arg, arg2, ...)

Function.prototype.call2 = function (context) {
context = context ? Object(context) : window;
context.fn = this
var args = [...arguments].slice(1);
var result = context.fn(...args);
delete context.fn
return result
}

// apply
// .apply(context, arr)
Function.prototype.apply2 = function (context, arr) {
context = context ? Object(context) : window;
context.fn = this;
if (arr instanceof Array) {
var result = context.fn(...arr)
} else {
var result = context.fn()
};
delete context.fn
return result
}

// bind
// 返回一个函数 可以链式调用参数
// 用构造函数的时候 返回最开始函数的this

Function.prototype.bind2 = function (context) {
context = context ? Object(context) : window;
var args = [...arguments].slice(1)
var self = this
return function () {
self.apply(context, args.concat([...arguments]))
}
}

Function.prototype.bind2 = function (context) {
context = context ? Object(context) : window;
var args = [...arguments].slice(1)
var self = this
var bound = function () {}
bound.prototype = this.prototype
var band = function () {
return self.apply(this instanceof bound
? this : context,
args.concat([...arguments]))
}
band.prototype = new bound()
return band
}

模拟实现new

new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例。 ——(来自于MDN)

function create() {
var func = [].shift.call(arguments);
var obj = new Object();
obj.proto = func.prototype;
var res = func.apply(obj, arguments);
return res instanceOf Object ? res : 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.