Copy the codeThe code is as follows:
var obj = ("name");
function clickMe() {
alert();
+= "!!!!";
alert();
}
var ActionBinder = function() {//Define a class
}
= function(doms) {
= doms;//Register doms
}
= function(handlers) {
= handlers;//Register an action
}
= function() {
=
}//Register doms actions
var binder = new ActionBinder();//Create a new class according to ActionBinder method
(obj);
(clickMe);
();
First, we will first set up an object-oriented code written in js, and first create an ActionBinder class, which is also written in Java; because js is based on html dom object to operate the content of html, define a registered dom method registerDOM in the class, and use prototype to prototype the method to facilitate call; in addition, a registered event method registerAction is added, and it is also prototyped with a prototype method; finally, a prototype action bind bind bind to bind the registered dom and the registered events and execute it.
Another original js code snippet:
Code
Copy the codeThe code is as follows:
<body>
<script>
= function(){
var obj = ("name");
= function(){alert();}
}
</script>
<input type="text" />
</body>
The code also achieves the desired effect. For some simple applications, the above effect can be satisfied, but for some more complex programs, it is more troublesome to apply and write in the code is also more cumbersome; such as code snippets
Code
Copy the codeThe code is as follows:
<body>
<script>
= function(){
obj1 = ("name1");
obj2 = ("name2");
obj3 = ("name3");
= function(){alert();}
= function(){alert();}
= function(){alert();}
}
</script>
<input type="text" value="111" />
<input type="text" value="222" />
<input type="text" value="333" />
</body>
or
Code
Copy the codeThe code is as follows:
<body>
<script>
function clickMe(){alert();}
</script>
<input type="text" value="111" onclick="return clickMe()" />
<input type="text" value="222" onclick="return clickMe()" />
<input type="text" value="333" onclick="return clickMe()" />
</body>
Of course, there are other simpler writing methods for the above two codes, and in general, there are still a lot of redundant codes.
It is more flexible to write in an object-oriented method, such as
Code
Copy the codeThe code is as follows:
<body>
<script>
= function() {
var objs = ("input");
function clickMe() {
alert();
}
var ActionBinder = function() {//Define a class
}
= function(doms) {
= doms;//Register doms
}
= function(handlers) {
= handlers;//Register an action
}
= function() {
=
}//Register doms actions
for (var i=0;i<;i++ ){
var binder = new ActionBinder();//Create a new class according to ActionBinder method
(objs[i]);
(clickMe);
();
};
}
</script>
<input type="text" value="111"/>
<input type="text" value="222"/>
<input type="text" value="333"/>
</body>
In this way, there will be no redundant code, and the js is logically refreshing, so the binding of multiple events remains to be studied.