


The difference between two implementations of defining prototype methods in JS classes_javascript skills
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()
{
}
Extends prototype method:
JSClass.prototype.MethodA = function()
{
};
Or
function = JSClass.prototype.MethodA()
{
};
In fact, the definition of these two prototypes can be simplified and discussed. First, they are regarded as two functions, as follows:
Foo1();
function Foo1()
{
alert(This is Foo1.);
}
and Foo2();
var Foo2 = function()
{
alert(This is Foo2.);
}
There will obviously 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, which is easy to understand. If the function is not processed first, there will be no way to handle the function calls in the function. If we first define fn1() and then define fn2(), but call fn2 from fn1, then the parsing will not pass. Why Foo2 cannot 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 entirely because it points to an instance of a function object. That’s all.
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();
<script>
<br>function NormalClass()
<br>{
<br> this.m_Property1 = ’P1 in Normal Class.’;
<br> this.m_Property2 = ’P2 in Normal Class.’;
<br><br> this.toString = function()
<br> {
<br> return ’[class NormalClass]’;
<br> }
<br><br> return new InnerClass();
<br><br> function InnerClass()
<br> {
<br> this.m_Property1 = ’P1 in Inner Class.’;
<br> this.m_Property2 = ’P2 in Inner Class.’;
<br><br> this.toString = function()
<br> {
<br> return ’[class InnerClass]’;
<br> }
<br> }
<br><br> InnerClass.prototype.Method1 = function()
<br> {
<br> alert(this.m_Property1);
<br> };
<br><br> function InnerClass.prototype.Method2()
<br> {
<br> alert(this.m_Property2);
<br> };
<br>}
<br></script> What is the effect? Why?

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.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

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.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

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
