Javascript JS匿名函数
我的问题是我想调用如下函数:Javascript JS匿名函数,javascript,html,anonymous-function,anonymous,Javascript,Html,Anonymous Function,Anonymous,我的问题是我想调用如下函数: $('div').doSomething('xyz'); 我的js代码是: var $ = function(element) { var doSomething = function(xyz, xzy, zxy) { alert(xyz + element); }; }; 但这不起作用(我不熟悉js匿名函数),错误在哪里 谢谢你的帮助 试试看 var $ = function(element) { // if the f
$('div').doSomething('xyz');
我的js代码是:
var $ = function(element) {
var doSomething = function(xyz, xzy, zxy) {
alert(xyz + element);
};
};
但这不起作用(我不熟悉js匿名函数),错误在哪里
谢谢你的帮助 试试看
var $ = function(element) {
// if the function is called without being called as a constructor,
// then call as a constructor for us.
// (partially borrowed from http://stackoverflow.com/questions/4556110/creating-a-jquery-like-object )
if (this.constructor !== $) {
return new $(element);
}
this.doSomething = function(txt) {
alert(txt + element);
};
};
请参阅firebug输出:$('div')是undefined@idealmachine已编辑以使用您的注释进行修复。哦,在IE中不起作用-它不支持非标准协议。谢谢你的帮助!我试图删除
\uuuuu proto\uuuuu
以直接访问这个.constructor
,它现在似乎在Chrome和IE中都能工作。