


There are many comments on the difference between the two implementations of JS class definition prototype method_js object-oriented
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:
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~~~

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

This article explores effective use of Java's Collections Framework. It emphasizes choosing appropriate collections (List, Set, Map, Queue) based on data structure, performance needs, and thread safety. Optimizing collection usage through efficient

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion
