JavaScript Bind Fun Solution

Source: Internet
Author: User

First of all, this answer is seen from Segmentfault, very interesting to record. I put it to the bottom:

bind ()

Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

Function.prototype.bindThis method is ECMAScript 5 new additions, in Firefox 4/chrome support, IE8 should not be supported.

Brief introduction:

Creates a function that, when the new function is called, uses the Context of the given this value and passes in the given argument sequence.

Grammar:

var bound = fun.bind( thisValue, [, arg1 [, arg2 [ ... ] ] ] );

Parameters:

thisValue: When a new Function is called, thisValue its value is paid this . If Function is called using the new operator, it is ignored thisValue .

arg1, arg2, ...: When a new Function is called, it is passed in as a parameter list, inserted before the actual argument at the time of invocation.

--------------------------------------------------I'm a cute split-line--------------------------------------------------------------- --------

Give me a chestnut:

Link here: https://segmentfault.com/q/1010000002508005

When you (son) and your father (Papa) in a piece of time, everyone will know your father's name (do not know can directly ask Ah!)    ), in code: var papa = {name: "Li Gang", Son:function () {return this.name; }};alert (Papa.son ()); But when you're not with your dad? This is the case: var son = Papa.son;alert (son ()); You will find that you have no way to know your father's name, and then you can't do it. Fortunately, you remember your father's phone number, make a phone call can also make waves and know: alert (Son.call (Papa)); then you think, no ah, I can not every time I have to call my father, my father will not kill me. Eh So stupid, I carry my father's ID card with my dad is not good, so you have become this: son = Son.bind (papa), Alert (Son ()); Haha, dad no longer have to worry about me going out to be beaten (? ´? '?)! In this way, after half a year, your father died and received an adopted son! What the! The estate is all mine! The Chinese name of the adopted son is two dog, the English name is Er~gou~zi:function Ergouzi () {return this.name;} This two dog son also wants to know your father's name to go out to the girls to sing the praises of the sister, begged for a long while, dad also gave him a copy: Ergouzi = Ergouzi.bind (papa); alert (Ergouzi ()); Two dogs, this is a good thing. The decision to give the father's name every time to add the love of the three words, as we often say beloved Prime Minister X, so he did a little wit a small surgery: Ergouzi = function () {var prefix = arguments[0] | |    ""; return prefix + this.name;} Ergouzi = Ergouzi.bind (Papa, "great"); alert (Ergouzi ()); It was not long before dad knew about it. Dad is very satisfied with the behavior of the two dog licking heels, more and more affectionate to him, more and more bench to you. Finally, Dad gave all the legacy to two dog son and you because usually only swindling what skills will not be able to go to the streets to beg the last to be beaten and killed by Chengguan. End!

  

Finally in put a about null and undefined, this comes from Ruan teacher undefined and null difference article in the comments, interested friends can go to see.

Null

Q: Do you have Zhang San this person?
A: there!
Q: Where is Zhang San?
A: Already dead.

Undefine:

Q: Do you have Zhang San this person?
A: No!

JavaScript Bind Fun Solution

Related Article

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.