了解javascript函数用法

时间:2016-02-22 19:42:59

标签: javascript

我想了解javascript中的以下代码片段。我试图谷歌检查以下格式创建JavaScript函数的用法,但我没有找到任何参考。

以下是我对代码的理解和plz更正,如果这是代码,并欣赏任何参考指南,以检查更多关于此类使用符号。

  1. function(obj) - >这基本上是在全局范围内运行一个函数,该函数在加载DOM后在页面加载上运行。
  2. obj['test'] = 'DummyObj';这是创建一个全局变量。我想我确信这个用法。
  3. obj['test1'] = obj['test1'] || function(){ (obj['test1'].h = obj['test1'].h||[]).push(arguments) }, obj['test1'].m = 1 * new Date()
    我很难理解这一点。我的分析是,这是检查test1对象是否为null它正在创建一个函数,并且在该函数中它正在检查'h'对象,如果它是null,则它创建一个空数组并推送本地'arguments'对象。我不明白我们有一个逗号和日期对象的第二部分是什么?这是否意味着它将作为一个语句执行并创建一个具有当前日期值的'm'局部变量?
  4. 我们正在使用的最后一部分(窗口)是我完全不理解的。这是什么意思?能否指导我在哪里进一步阅读
  5. (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);
    

2 个答案:

答案 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);