Object.prototype.bind = function (key, obj, prop, event) { var that = this if (this[key] == undefined) this[key] = ''; var bc = undefined; if (!this.__bc__) { this.__bc__ = {}; Object.defineProperty(this, "__bc__", { enumerable: false }); } if (!this.__bc__[key]) { this.__bc__[key] = []; this.__bc__[key].value = this[key] bc = this.__bc__[key]; Object.defineProperty(this, key, { get: function () { return bc.value }, set: function (value) { if (bc.value == value) return; bc.forEach(function (l) { l(value); }) bc.value = value } }) } bc = this.__bc__[key]; if (prop == undefined) prop = "value"; if (obj[prop] == undefined) prop = 'innerHTML'; if (event == undefined) event = 'change'; if (typeof obj == 'string') { var els = document.querySelectorAll(obj); for (var i = 0; i < els.length; i++) { var el = els[i]; bc.push(function (value) { el[prop] = value; }); if (el.addEventListener) { el.addEventListener(event, function (e) { that[key] = el[prop]; }) } el[prop] = bc.value; } } else if (typeof obj == 'function') { bc.push(obj); obj(bc.value); } else if (typeof obj == 'object' && obj.toString().indexOf("HTML") == 8) { bc.push(function (value) { obj[prop] = value; }) obj[prop] = bc.value; if (obj.addEventListener) { obj.addEventListener(event, function (e) { that[key] = obj[prop]; }) } } else { console.log("obj = " + obj + " 不是 [selector,function,element] 中的任何一种,绑定失败!") } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>测试项目</title> <script src="bind.js"></script> </head> <body> <input type="text" id="i1"> <input type="text" id="i2"> <script> var a = { value: 's' } a.bind('value', i1) a.bind('name', i2) </script> </body> </html>
可绑定到 selector,function,el
Object.prototype.bind = function (key, obj, prop, event) {
var that = this
if (this[key] == undefined) this[key] = '';
var bc = undefined;
if (!this.__bc__) {
this.__bc__ = {};
Object.defineProperty(this, "__bc__", { enumerable: false });
}
if (!this.__bc__[key]) {
this.__bc__[key] = [];
this.__bc__[key].value = this[key]
bc = this.__bc__[key];
Object.defineProperty(this, key, {
get: function () {
return bc.value
},
set: function (value) {
if (bc.value == value) return;
bc.forEach(function (l) { l(value); })
bc.value = value
}
})
}
bc = this.__bc__[key];
if (prop == undefined) prop = "value";
if (obj[prop] == undefined) prop = 'innerHTML';
if (event == undefined) event = 'change';
if (typeof obj == 'string') {
var els = document.querySelectorAll(obj);
for (var i = 0; i < els.length; i++) {
var el = els[i];
bc.push(function (value) { el[prop] = value; });
if (el.addEventListener) {
el.addEventListener(event, function (e) {
that[key] = el[prop];
})
}
el[prop] = bc.value;
}
} else if (typeof obj == 'function') {
bc.push(obj);
obj(bc.value);
} else if (typeof obj == 'object' && obj.toString().indexOf("HTML") == 8) {
bc.push(function (value) { obj[prop] = value; })
obj[prop] = bc.value;
if (obj.addEventListener) {
obj.addEventListener(event, function (e) {
that[key] = obj[prop];
})
}
} else {
console.log("obj = " + obj + " 不是 [selector,function,element] 中的任何一种,绑定失败!")
}
}