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);
}