Home >Web Front-end >JS Tutorial >js programming notes nameless function_javascript skills

js programming notes nameless function_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:05:231009browse

1. (function(){alert("hello");})()

2. Unnamed function

Unnamed function, one of its functions may be to generate a reference to a new function object, Mainly for definition.

Another use is for some callback functions in js that cannot contain parameters.
The obvious example is setInterval. I think this is a function that many people have a headache with, especially when you want to add parameters to the callback function.
And the most troublesome thing is that DHTML is not a standard specified by w3c, so different browsers give different setInterval parameter tables. . .
As for the two browsers I tested (IE core, webkit core)
IE: setInvterval(function, msecond [,lang]);
chrome:setInterval(function, msecond [, pram1, pram2, ....]);
In other words, chrome allows adding parameters to functions, and the parameter list is at the end. However, the last parameter of IE is to indicate the type of scripting language used, because in addition to js, ​​IE also supports other scripting languages ​​such as vbs.
In order to solve the problem of compatibility, we have to use unnamed functions. . .
[code]
function test(yourArg)
{
var arg = yourArg;
setInterval(function(){callback(arg)}, time);
}
[html]

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn