The bind method replaces the closure.

Source: Internet
Author: User
<A href = "JavaScript:"> a </a> <a href = "JavaScript:"> B </a> <a href = "JavaScript: "> c </a> <a href =" JavaScript: "> d </a> <a href =" JavaScript: "> e </a> <script> var I, s = document. querySelectorAll ("a"); for (I = 0; I <s. length; I ++) s [I]. addEventListener ("click", function () {alert (this); // The first parameter of bind can be obtained using this in the function }. bind (I)/* Here is a parameter for the anonymous callback function bind */); </script>

The bind here is not an event, but a method to bind the function to the default parameter.

<Script type = "text/javascript"> var f, o, fo, name = "SB"; o = {name: "Sub-cobalt carbonate"}; f = function (e) {console. log (this. name + ":" + e)}; fo = f. bind (o, "trembling, human! "); F (" The parameter is valid when called directly! "); Fo (" The parameter here will not be passed to e! "); </Script>

The code shows that the bind changes the original function parameter.

The bind method replaces the closure.

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.