Js class definition

Source: Internet
Author: User

The first problem encountered is the encapsulation of js objects. js does not provide a class mechanism. The only built-in class is the function class. That is to say, all functions are the instantiated objects of the function class. However, with this unique class, we can simulate and define a new class.
The first thing that comes to mind is to directly use the function to generate a fully defined class:
Copy codeThe Code is as follows:
Function myClass (arg ,...)
{
This. attributeName;
This. functionName = function (){};
}

However, there is a problem that whenever I create a new myClass instance, the internal function will re-open the space and return the reference to functionName. However, this is inconsistent with the class we imagined and wastes space. In theory, the function of the class should be shared.

A more reasonable approach is to define a function outside the class, and assign the function pointer to functionName in the class, and the other is myClass outside the class. prototype. functionName = function (){}. Both of them are good choices, and the second one looks closer to the class definition.
Var newObj = new myClass (); success.

About js (2) unknown functions
An untitled function may be used to generate a reference to a new function object, which is mainly used for definition.
Another use is for callback functions that cannot contain parameters in js.


The obvious example is setInterval. I think this is a headache for many people, especially when you want to add parameters to the callback function.
In addition, DHTML is not a w3c standard, so the setInterval parameter table provided by different browsers is different...
For the two browsers I tested (IE kernel, webkit kernel)
IE: setInvterval (function, msecond [, lang]);
Chrome: setInterval (function, msecond [, pram1, pram2,...]);
That is to say, chrome allows parameters to be added to the function, and the parameter table is at the last side. However, the last parameter of IE is used to indicate the types of scripting languages used, because IE supports other scripting languages such as vbs in addition to js.

To solve the compatibility problem, we have to use the unknown function...
Copy codeThe Code is as follows:
Function test (yourArg)
{
Var arg = yourArg;
SetInterval (function () {callback (arg)}, time );
}

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.