Home > Web Front-end > JS Tutorial > There are many comments on the difference between the two implementations of JS class definition prototype method_js object-oriented

There are many comments on the difference between the two implementations of JS class definition prototype method_js object-oriented

WBOY
Release: 2016-05-16 19:08:59
Original
964 people have browsed it

We know that adding prototype methods to JavaScript classes is very simple. And the following two methods are commonly used, but are there any differences in the use of these two methods?
JScript Class:

Copy code The code is as follows:

function JSClass()
{ >

JSClass.prototype.MethodA = function()
{

}; Copy code


The code is as follows:


function = JSClass.prototype.MethodA()
{

};

# re: The difference between the two implementations of JS class definition prototype methods Reply More comments
Let me first talk about a simple difference: the prototype method imported by these two methods, the first one is an anonymous method; The second method has the method name "JSClass.prototype.MethodA".
2005-03-01 10:52 | birdshome
# re: The difference between two implementations of JS class definition prototype method Reply More comments

<script> <br>function JSClass() <br>{ <br>} <br><br>function = JSClass.prototype.MethodA() <br>{ <br><br>}; <br></script>


Prompt error.
2005-03-01 13:51 | 阮
# re: The difference between the two implementations of JS class definition prototype methods Reply More comments
faint, I found that FreeTextBox modifies a small amount of data (one or two characters )Submission sometimes has no effect:(
I accidentally wrote an extra "=", but I remember I modified it.
2005-03-01 14:00 | birdshome
# re: The difference between the two implementations of JS class definition prototype methods Reply More comments
In fact, these two prototype definition methods can be simplified for discussion. First, treat them 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 the function definition (Foo1) has the highest initialization priority in the script parser. This is easy to understand. If the function is not processed first, then for the function. There is no way to deal with function calls. If we first define fn1() and then define fn2(), but call fn2 from fn1, then the parsing will not pass. Why can't Foo2 be initialized? The definition of Foo2 is not a function definition at all. It is a standard assignment statement. The reason why Foo2(Foo2()) can be used like a standard function is because it points to an instance of a function object.

2005-03-03 22: 17 | birdshome
# re: The difference between the two implementations of JS class definition prototype method Reply More comments
Let’s look at the two ways to import the prototype method, it’s very simple.And different execution priorities also determine the differences in their use. See the following example:


Execution:
var nc = new NormalClass();
nc.Method1();
nc.Method2();

What is the effect? Why?


2005-03-03 22:21 | birdshome
# re: The difference between two implementations of JS class definition prototype methods Reply More comments
The final result is actually nc.Method1 () is not defined, nc.Method2() runs normally.
In fact, it is not surprising that InnerClass.prototype.Method1 = function() relies on the execution of the assignment statement, while function InnerClass.prototype.Method2() is initialized by the script engine with the highest priority.
2005-03-05 02:43 | birdshome
# re: The difference between the two implementations of JS class definition prototype method Reply More comments
I tested your code in Antechinus JavaScript Editor:

function InnerClass.prototype.Method2() reports an error,

SyntaxError:missing( before formal parameters See: .prototype.Method2(
2005-05-10 17:11 | Error
# re: The difference between the two implementations of JS class definition prototype method Reply More comments
@Error
Have you tried it with IE?
2005-05-10 17:30 | birdshome
# re: The difference between the two implementations of JS class definition prototype methods Reply More comments
I also get the same error when using FF: missing( before formal parameters See: .prototype.Method2(
2006-08-19 22: 40 | jzz
# re: The difference between the two implementations of JS class definition prototype methods Reply More comments
return new InnerClass(); Move this line to
function InnerClass.prototype.Method2()
{
alert(this.m_Property2);
};
The subsequent ie execution is normal. FF error report: missing( before formal parameters See: .prototype.Method2(
ie is executed sequentially Yes, but the NS series is not!
When FF executes function InnerClass.prototype.Method2(), it does not know that there is this InnerClass class at all, so naturally it cannot come up with something like prototype.xxx for no reason
2006-11-13 00:57 | Doutu
# re: The difference between the two implementations of JS class definition prototype method Reply More comments
@Doutu
Put return new InnerClass(); into function After the InnerClass.prototype.Method2() method, it completely violates my original intention of writing this example.This example just illustrates that IE has a higher parsing priority for the function definition format function foo(), while foo = function() is just an ordinary assignment statement. As for the situation in ff, I have not studied it. Since you said that ff cannot find the InnerClass after return, it means that the sequential parsing function foo() is a defined format.
2006-11-13 01:29 | birdshome
# re: The difference between two implementations of JS class definition prototype method Reply More comments
Sigh. Poor people who can only use ie. The writing method of function x.y.z() {} is simply non-standard writing method. It is only supported by ie. Other js engines such as ff or opera will report errors. The only writing method that meets the standard is x.y.z = function () {};

Of course, from a grammatical level, I quite like this writing method and hope that future standards will adopt this writing method.
2006-11-28 11:04 | hax
# re: The difference between two implementations of JS class definition prototype method Reply More comments
Haha hax is right. Only IE will tolerate all kinds of mistakes of children like a mother

The standard writing method is only x.y.z = function () {};

In fact, IE also supports more weird writing methods.

Look at this
function window::onload(){
alert("go_rush")
}
2006-11-28 14:39 | Go_Rush
# re: The difference between the two implementations of the JS class definition prototype method Reply More comments
@hax
No matter how good the standard is, it still serves people. The debate on this is a matter between academics and engineers. Let’s implement it well Our own system is enough, why bother fighting with gods too much.
// Your comment is actually quite good. Sigh, it’s a pity that because of IE, I’m so pitiful~~~
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template