[removed] final / immutable global variables?

前端 未结 14 2102
既然无缘
既然无缘 2020-12-13 09:03

I think I know the answer but... is there any way to prevent a global variable from being modified by later-executing

14条回答
  •  没有蜡笔的小新
    2020-12-13 09:30

    You might want to try out this jquery plugin. It prevents you to create global objects in javascript :)

    example code

    Store data

    // 'val' can be a string, integer, hash table, array, object
    $.secret( 'in', 'secretName', val );
    
    // or a function
    $.secret( 'in', 'secretName', function( arg1, arg2, arg3 ){
      // do something here
    });
    

    Use data; you can even use it in different files.

    var lang = $.secret( 'out', 'lang' );
    

    Call out a function

    $.secret( 'call', 'secretName', [ arg1, arg2, arg3 ]);
    // or
    $.secret( 'call', 'secretName', arg );
    

    Clear data

    $.secret( 'clear', 'lang' );
    

    source code is on github

提交回复
热议问题