我想了解javascript中的以下代码片段。我试图谷歌检查以下格式创建JavaScript函数的用法,但我没有找到任何参考。
以下是我对代码的理解和plz更正,如果这是代码,并欣赏任何参考指南,以检查更多关于此类使用符号。
function(obj)
- >这基本上是在全局范围内运行一个函数,该函数在加载DOM后在页面加载上运行。obj['test'] = 'DummyObj';
这是创建一个全局变量。我想我确信这个用法。obj['test1'] = obj['test1'] || function(){
(obj['test1'].h = obj['test1'].h||[]).push(arguments)
},
obj['test1'].m = 1 * new Date()
(function(obj) {
obj['test'] = 'DummyObj';
obj['test1'] = obj['test1'] || function(){
(obj['test1'].h = obj['test1'].h||[]).push(arguments)
},
obj['test1'].m = 1 * new Date();
})(window);
答案 0 :(得分:0)
我拿了你发布的代码并用一些评论来标记它,试图描述发生了什么。
cat file.txt | perl -F' ' -e 'print $F[1]."\n"'
总的来说,我会说这段代码很神秘,很丑陋。例如,在与// wrap code in a self-executing function
// this creates a scope-boundary, but since you are not using
// the "var" keyword anywhere it doesn't matter much
// the "obj" argument is the global "window" variable
// made available by the execution content (most-likely your web-browser)
(function(obj) {
// create a "test" variable that live off of the "window" object
// so: window.test = 'DummyObj';
obj['test'] = 'DummyObj';
// if window.test1 is "truthy" then this will be a no-op
// otherwise, assign window.test1 to a function
obj['test1'] = obj['test1'] || function(){
// if window.test1.h doesn't have a value,
// assign it to an empty array
// Then, add the value of window.test1.h to the
// implicit "arguments" variable that comes with each function
(obj['test1'].h = obj['test1'].h||[]).push(arguments)
}; // NOTE: I changed the comma here to a semi-colon
// set window.test1.m to the unix epoch integer value (in ms)
obj['test1'].m = 1 * new Date();
})(window);
相同的语句中将值分配给数组。手动推送到push
数组是另一个。
我不建议使用此代码snippit来学习JavaScript,因为它可能会教你一些反模式。
答案 1 :(得分:0)
此模式(function(x) {})(y);
称为立即调用的函数表达式(IIFE),通常用于创建范围。
至于其余的,这是可怕的垃圾。分析它并不是非常困难,但它的真的不清楚为什么有人会这样做。
这是一次又一次的打击:
//create a scope with the function wrapper, pass in the global
//window object under the alias 'obj'
(function(obj) {
//assign sting 'DummyObj' to the 'test' property of
//the global object, effectively creating a global variable
//and totally nullifying the reason for wrapping this in a
//function
obj['test'] = 'DummyObj';
//same thing but with the 'test1' global property/variable
//hese the logical or operator is used to conditionally
//assign the property. problem is *none* of this works,
//the following will throw:
obj['test1'] = obj['test1'] || function(){
//when called fn will attempt to conditionally assign
//an array to the h property of the test1 variable,
//ie the function itself. this is a bad idea. then it
//pushes the arguments on to that array. this is kinda
//sorta an attempt at making a function that caches its
//arguments, but it doesn't really work.
(obj['test1'].h = obj['test1'].h||[]).push(arguments)
},
//1 * new Date() will yield a millisecond timestamp, but
//+new Date() and Date.now() are both clearer. note that
//this code is also assigning a random property to the
//function object stored in test1
obj['test1'].m = 1 * new Date();
})(window);