SoFunction
Updated on 2025-04-14

JavaScript Object-Oriented Introduction to Literacy Page 2/2


C. Metagenesis:Encapsulate some commonly used methods with relatively high versatility in a function, and then assign them to the class that wants to use these methods through the following function. You can also selectively pass the required methods for different classes.
Copy the codeThe code is as follows:

<script type="text/javascript">
function agument(receveClass,giveClass){
if(arguments[2]){
var len = ;
for(i=2;i<len;i++){
[arguments[i]] = [arguments[i]];
}
}
else{
for(method in ){
if(![method]){
[method] = [method];
}
}
}
};
var Name = function(){};
={
sayLike:function(){
alert("i like oldfish");
},
sayLove:function(){
alert("i love oldfish");
}
}
var Fish = function(){};
var ioldFish = new Fish();
agument(Fish,Name,"sayLove");
();
();
</script>

Polymorphism: I personally think this is relatively abstract and difficult to express, so I will briefly explain it from the two aspects of overloading and covering.

Overload: In the above example, the agument function initially takes two parameters, but in the subsequent call, the agument(Fish,Name,"sayLove") can also bring any multiple parameters. The overload of javascript is implemented by the user in the function by operating the arguments attribute.

Overwrite: This is very simple. If the method defined in a subclass has the same name as the method inherited from the superclass, overwrite this method (this is not overwriting the method in the superclass, pay attention). This is not a burden here!

Finally, let’s focus on this and the execution context. In the encapsulation example mentioned above, this represents the instantiation object itself of the class where this is located, but it is not the same. For example, the event processing code defined by HTML attributes is shown in the following code:
Copy the codeThe code is as follows:

<script type="text/javascript">
var Name = function(name) {
= name;
= function () {
alert();
}
};
var ioldFish = new Name("old fish"),
btn = ('btn');
= ;
// = function(){(ioldFish)};
</script>

In the above example, after clicking the button, the attributes of the instance object are not displayed in the pop-up box. This is because the execution context of this has changed. The context he is in now should be the HTML tag input, but the attribute getName does not exist for this tag, so naturally it is impossible to output the attribute value of this attribute! From this example, it is not difficult to see that the execution context is determined only when it is executed, and it can change at any time.
Of course, you can remove the code I commented out above and change the execution context of this by calling to get the getName method. The apply method can also implement the function of changing the execution context, but a more beautiful implementation method bind is found in the prototype framework. Let’s take a look at the implementation of this method, and I have to sigh at the greatness of our ancestors…
Copy the codeThe code is as follows:

= function(obj) {
var method = this,
temp = function() {
return (obj, arguments);
};
}

I believe that if you can understand it, you can already write a simple script framework based on these knowledge points. Practice more. I believe that experts will be able to advance to the level in the near future.
Previous page12Read the full text