The bind () method is not applicable in ie, 6, 7, and 8. You need to extend the Functionprototype to implement this method. The following describes the functions of the bind function in javascript.
ButtonScript var button = document. getElementById ("button"); button. onclick = function () {alert (this. id); // pop-up button}; // you can see that the context of this is the button script
Bind
The Code is as follows:
Var text = document. getElementById ("text ");
Var button = document. getElementById ("button ");
Button. onclick = function (){
Alert (this. id); // a button is displayed.
}. Bind (text );
// You can see that the context of this is button
This changes to text.
The function can also be used in the literal to keep the up and down (this) unchanged.
var obj = {color: "#ccc", element: document.getElementById('text'),events: function() {document.getElementById("button").addEventListener("click", function(e) {console.log(this);this.element.style.color = this.color;}.bind(this))return this;},init: function() {this.events();}};obj.init();
Click the text button to change the color. This is not a button but an obj.
The bind () method is not applicable in ie, 6, 7, and 8. You need to extend the Function prototype to implement this method.
if (!Function.prototype.bind) {Function.prototype.bind = function(obj) {var slice = [].slice, args = slice.call(arguments, 1), self = this, nop = function() {}, bound = function() {return self.apply(this instanceof nop ? this : (obj || {}),args.concat(slice.call(arguments)));};nop.prototype = self.prototype;bound.prototype = new nop();return bound;};}
Now we can see that ie6, 7, and 8 also support bind ().
The Code is as follows:
Slice = Array. prototype. slice,
Or
Array = Array. prototype. slice. call (array, 0 );
Convert a similar array to an array