SoFunction
Updated on 2025-04-12

Research on prototype attributes in JScript, page 2/2


In the last article, I listed the various uses of the prototype attribute in JScript, but the prototype was not created by JScript. JScript actually uses a derivative form of the prototype pattern in our design pattern. Let me briefly talk about the prototype pattern below, and then see what the prototype in JScript is going on?!
What's prototype pattern?
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Use prototype instances to specify the type of objects to be created, and create new objects by copying these prototypes.
Prototype mode allows an object to create another customizable object without knowing any details of how to create it. The working principle is: by passing a prototype object to the object to be created, the object to be created by requesting the prototype object to be copied and they are created by requesting that the prototype object be copied by itself.
To continue to understand what prototype pattern is, you can refer to the article 'Prototype of Design Pattern'. It doesn't matter if you don't understand Java. Just treat its code as C#.
Have you figured out what the prototype is? Anyway, remember one thing, the implementation of prototype pattern depends on the clone operation. Of course, it depends on your needs of shallow copy or deep copy clone.
Let's continue to talk about the prototype in JScript. Why do we say it is different from the prototype in the prototype pattern?! This is not something I say, nor is it something I blow it up. Look at this example, you will be confused:
Copy the codeThe code is as follows:

<script language="javascript">
function RP()
{
= 1;
= function()
{
alert(" ");
};
= 100;
= function()
{
alert("");
};
}
= 10;
= function()
{
alert("");
};
</script>

Don't worry, I haven't started making examples yet, I just gave a class that we used to demonstrate. What is RP? rpwt? Of course not, RP is ResearchPrototype. OK, don’t talk nonsense, look at the examples and results analysis.
Copy the codeThe code is as follows:

<script language="javascript">
rp = new RP();
alert();
();
alert();
();
</script>

The results of the operation are coming out:
1

100

This %$@#^$%&^..., don’t worry, keep watching!
Copy the codeThe code is as follows:

<script language="javascript">
rp = new RP();
delete ;
alert();
delete ;
();
delete ;
alert();
delete ;
();
</script>

The operation results are released again:
undefined
A Runtime Error has occurred.
Do you wish to Debug?
Line: 32
Error: Object doesn't support this property or method
10

It's fun, can you tell me what's the point? The sum here is only used as a reference, but how can the sum be deleted and the result can be produced, and it is also the properties and methods imported by prototype?
This is the biggest difference between JScript's prototype and prototype pattern. The so-called prototype property in JScript is actually a feature supported by the language itself. No copy occurs here, whether it is shallow or deep. For JScript's interpretation engine, when it processes the properties and methods of the object referenced by "." or "[keyName]", it first looks in the instance of the object itself (this) and returns or executes if it is found. If it is not found, it will look for whether the object and method being found in the prototype() of the object is defined. If it is found, it will be returned or executed. If it is not found, it will be returned undefined (for attributes) or runtime error (for methods).
Because the properties or methods of the prototype import class instance are dynamically searched, we can add prototype properties and methods to the internal objects of the system, such as adding a trim method to the String object:
Copy the codeThe code is as follows:

<script lanuage="javascript">
()
{
return (/(^\s+)|(\s+$)/g, "");
}
</scritp>

Obviously, this usage in JScript is also something that the prototype in the prototype pattern cannot be explained and supported.
Now there should be no obstacles in understanding the prototype inheritance method in JScript OOP? At the same time, you should also understand why the prototype inheritance method has such a big natural flaw, right? Of course, if you have any questions, please continue to discuss.
Attached the demo sample source code:

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
Previous page12Read the full text