Differences between the two implementations of JS class definition prototype methods many of us know that it is very easy to add prototype methods to JavaScript classes. The following two methods are commonly used. Are there any differences between the two methods?
JScript Class:
The code is as follows:
Function JSClass ()
{
}
Extends prototype method:
The code is as follows:
JSClass. prototype. MethodA = function ()
{
};
Or
The code is as follows:
Function = JSClass. prototype. MethodA ()
{
};
# Re: differences between the two implementations of the JS class definition prototype method
Let me first make a simple difference: The first method is an anonymous method, and the second method is named "JSClass. prototype. MethodA ".
| Birdshome
# Re: differences between the two implementations of the JS class definition prototype method
Script
Function JSClass ()
{
}
Function = JSClass. prototype. MethodA ()
{
};
Script
An error occurred.
| Success
# Re: differences between the two implementations of the JS class definition prototype method
Faint, I found that when FreeTextBox modifies a small amount of data (one or two characters), it sometimes does not work :(
I wrote "=" by mistake, but I remember I modified it.
| Birdshome
# Re: differences between the two implementations of the JS class definition prototype method
In fact, the two original definition methods can be simplified and discussed first. they are considered as two functions, as follows:
Foo1 ();
Function Foo1 ()
{
Alert ('this is Foo1 .');
}
And Foo2 ();
Var Foo2 = function ()
{
Alert ('this is Foo2 .');
}
Obviously there will be no errors when running the first one, but there will be problems when running the second one. at this time, the system will say: Microsoft JScript runtime error: Object expected. This means that function definition (Foo1) has the highest initialization priority in the script parser, which is easy to understand. If the function is not processed first, there is no way to deal with the function calls in the function. if we first define fn1 () and then define fn2 (), but call fn2 from fn1, the resolution will fail. Why can't Foo2 be initialized? Foo2 is not a function definition at all. it is a standard value assignment statement. Foo2 (Foo2 () can be used like a standard function ()), this is because it points to an instance of a function object.
| Birdshome
# Re: differences between the two implementations of the JS class definition prototype method
Let's take a look at the two import methods in the original method. Different execution priorities also determine their differences in use. See the following example: