Bind-June can't kill a static object.

Source: Internet
Author: User

Bind's not a new thing anymore. I've been using the arrow function instead of some of its functions and I think it's pretty simple. Not doing too much research.

Recently found bind will help you do strange things not only bind the following

Const x = X=>{x};x.prop = ' x '; const x2 = X.bind ({});//undefinedconsole.log (X2.prop)

found that the static properties on the X function were removed this is not what I want! Sometimes we just want the static properties on the function to be inherited!

You can do this. Throw attributes into the upper prototype chain

Const X = x=>{x};x.__proto__ = {prop: ' x ', __proto__:function.prototype}const x2 = X.bind ({});//xconsole.log (X2.prop)

In an environment that does not support ES5, we can look back at the code as follows (from MDN) in Polyfill implementation.

There's a difference between this and the native bind.

And the above code doesn't satisfy you because you don't want static objects to be killed! You have to do something!

In fact, as long as the above on the basis of adding a sentence on the line as follows

fbound.__proto__ = {__proto__:this. __proto__};

That's it.

Bind-June can't kill a static object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.