We know that it's easy to add a prototype (prototype) method to a JavaScript class. And there are two methods commonly used, but are there any differences in the use of these two methods?
JScript Class:
Copy Code code as follows:
Extends Prototype Method:
Copy Code code as follows:
JSClass.prototype.MethodA = function ()
{
};
Or
Copy Code code as follows:
function = JSClass.prototype.MethodA ()
{
};
# Re:js class defines the difference between two implementations of a prototype method reply more comments
Let me start with a simple distinction: the prototype method that these two methods import, the first is an anonymous method, and the second method has the method name "JSClass.prototype.MethodA".
2005-03-01 10:52 | Birdshome
# Re:js class defines the difference between two implementations of a prototype method reply more comments
<BODY>
<script>
function Jsclass ()
{
}
function = JSClass.prototype.MethodA ()
{
};
</script>
</BODY>
Hint error.
2005-03-01 13:51 | Ruan
# Re:js class defines the difference between two implementations of a prototype method reply more comments
Faint, I found that freetextbox modifying a small amount of data (one or two characters) commits sometimes has no effect: (
I wrote a "=" in my hand, but I remember that I had changed it.
2005-03-01 14:00 | Birdshome
# Re:js class defines the difference between two implementations of a prototype method reply more comments
In fact, these two prototype definitions can be simplified to discuss, first think of them as two functions, as follows:
Foo1 ();
function Foo1 ()
{
Alert (' This is Foo1. ');
}
and Foo2 ();
var Foo2 = function ()
{
Alert (' This is Foo2. ');
}
Running the first one obviously doesn't make any mistakes, but running the second one is problematic, and the system says: Microsoft JScript runtime error:object expected. This means that the function definition (FOO1) has the highest initialization priority in the script parser, which is well understood. If you do not prioritize functions, then there is no way to handle function calls in functions, if we first set FN1 () and then define the FN2 (), but from the fn1 fn2, then pass the resolution. Why Foo2 can not be initialized, the definition of FOO2 is not a function definition at all, it is a standard assignment statement, and it is able to use Foo2 (FOO2) like a standard function simply because it points to an instance of a function object.
2005-03-03 22:17 | Birdshome
# Re:js class defines the difference between two implementations of a prototype method reply more comments
To see the prototype method in the two ways to import, it is very simple. And the different execution priorities, and the differences in their use, see the following example:
<script language= "JavaScript" >
function Normalclass ()
{
This.m_property1 = ' P1 in Normal Class. '
This.m_property2 = ' P2 in Normal Class. '
this.tostring = function ()
{
Return ' [Class Normalclass] ';
}
return new Innerclass ();
function Innerclass ()
{
This.m_property1 = ' P1 in Inner Class. '
This.m_property2 = ' P2 in Inner Class. '
this.tostring = function ()
{
Return ' [Class Innerclass] ';
}
}
InnerClass.prototype.Method1 = function ()
{
alert (this.m_property1);
};
function InnerClass.prototype.Method2 ()
{
alert (This.m_property2);
};
}
</script>
Perform:
var NC = new Normalclass ();
nc. Method1 ();
nc. METHOD2 ();
What's the effect? Why?
2005-03-03 22:21 | Birdshome
# Re:js class defines the difference between two implementations of a prototype method reply more comments
The final result is NC. METHOD1 () is not defined, NC. METHOD2 () is running normally.
Not surprisingly, InnerClass.prototype.Method1 = function () depends on the execution of the assignment statement, and the function InnerClass.prototype.Method2 () is initialized by the script engine with the highest precedence.
2005-03-05 02:43 | Birdshome
# Re:js class defines the difference between two implementations of a prototype method reply more comments
I test your code in the Antechinus JavaScript editor:
function InnerClass.prototype.Method2 () error,
Syntaxerror:missing (before formal parameters:. prototype. METHOD2 (
2005-05-10 17:11 | Error
# Re:js class defines the difference between two implementations of a prototype method reply more comments
@Error
Have you tried it with IE?
2005-05-10 17:30 | Birdshome
# Re:js class defines the difference between two implementations of a prototype method reply more comments
I use FF is the same error: missing (before formal parameters, prototype. METHOD2 (
2006-08-19 22:40 | Jzz
# Re:js class defines the difference between two implementations of a prototype method reply more comments
return new Innerclass (); Move this line to the
function InnerClass.prototype.Method2 ()
{
alert (This.m_property2);
};
Back IE performs normally. FF error: Missing (before formal parameters:. prototype. METHOD2 (
IE is in order to execute down, while NS series is not!
FF when executing to function InnerClass.prototype.Method2 () it does not know that there is this innerclass class, nature can not for no reason to a prototype.xxx dongdong
2006-11-13 00:57 | Doutu
# Re:js class defines the difference between two implementations of a prototype method reply more comments
@Doutu
After you put the return new Innerclass () to the function InnerClass.prototype.Method2 (), it completely violates my original intention of writing this example. This example just shows that IE has a higher resolution precedence for function foo (), and that foo = function () is a normal assignment statement. As for the situation in FF I have not studied, since you said that FF can not find the return of the Innerclass, then the explanation is the sequential parsing function foo () is also a defined format.
2006-11-13 01:29 | Birdshome
# Re:js class defines the difference between two implementations of a prototype method reply more comments
Alas. Poor guy who can only use IE. function x.y.z () {} of the writing is not standard, only IE support, other JS engine such as FF or opera will be an error. The standard notation is only x.y.z = function () {};
Of course, on the grammatical level, I quite like this kind of writing, I hope that the future standards can adopt this style.
2006-11-28 11:04 | Hax
# Re:js class defines the difference between two implementations of a prototype method reply more comments
Oh, hax right. Only IE can tolerate all the children's mistakes like mom.
The standard notation is only x.y.z = function () {};
In fact, IE also supports the more bizarre writing.
Check this out
function Window::onload () {
Alert ("Go_rush")
}
2006-11-28 14:39 | Go_rush
# Re:js class defines the difference between two implementations of a prototype method reply more comments
@hax
The standard again good, also serves for the people, the dispute this is the academic school and the engineering faction between the matter, we realize our own system to be possible, why excessively in the immortal fights.
Your comment is actually very good. Sigh, but unfortunately because of IE, I am so poor ah ~ ~ ~