About the definition of JS class _js object oriented

Source: Internet
Author: User
Encountered problems, first of all, JS object encapsulation, JS does not provide a class mechanism, the only built-in class is a function class, that is, all functions are the object of the function class instantiation. But depending on this unique class, we can simulate the definition of a new class.
The first thought is to define the complete class directly with function generation:
Copy Code code as follows:

function MyClass (Arg,...)
{
This.attributename;
This.functionname = function () {};
}

There is a problem, however, that whenever I create a new MyClass instance, the internal function will recreate the space and return the reference to functionname. But this is inconsistent with the class we envisage, wasting space, and theoretically the function of the class should be shared.

It is more reasonable to define a function outside the class, and then assign the function pointer to functionname within the class, and the other is myClass.prototype.functionName = function () {} outside the class. Both of these are good choices, and the second one looks closer to the definition of the class.
Next var newObj = new MyClass ();

About JS (ii) nameless function
Nameless function, in which one effect may be to generate a reference to a new function object, primarily for definition.
Another use is for JS in some of the parameters can not contain the callback function.


The obvious example is setinterval, which I think is a function of a lot of people's headaches, especially when you want to add parameters to the callback function.
And the most troubling thing is that DHTML is not the standard set by the Web, so different browsers give the SetInterval parameter table is not the same ...
For the two browsers I tested (ie kernel, webkit kernel)
Ie:setinvterval (function, Msecond [, Lang]);
Chrome:setinterval (function, Msecond [, pram1, Pram2, ...]);
In other words, the chrome inside is allowed to add parameters to the function, the parameter table on the last side. However, the last parameter of IE is to indicate the type of scripting language used, because IE also supports VBS and other scripting languages except JS.

In order to solve the compatibility, had to use the nameless function ...
Copy Code code as follows:

function test (YOURARG)
{
var arg = Yourarg;
SetInterval (function () {Callback (ARG)}, time);
}

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.