Differences between the two implementations of JS class definition prototype methods

Source: Internet
Author: User
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:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.