首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

JavaScript patterns 札记(一) 全局变量

2012-10-15 
JavaScript patterns 笔记(一) 全局变量myglobal hello // antipatternconsole.log(myglobal) //hel

JavaScript patterns 笔记(一) 全局变量

myglobal ="hello"; // antipattern

console.log(myglobal); //"hello"

console.log(window.myglobal); //"hello"

console.log(window["myglobal"]);// "hello"

console.log(this.myglobal); //"hello"

?

function sum(x, y) {

// antipattern: implied global

result = x + y;

return result;

function sum(x, y) {

var result = x + y;

return result;

// antipattern, do not use

function foo() {

var a = b = 0;

// ...

functionfoo() {

var a, b;

// ...

a = b = 0;// both local

}

// definethree globals

varglobal_var = 1;

global_novar= 2; // antipattern

(function (){

global_fromfunc= 3; // antipattern

}());

// attempt to delete

deleteglobal_var; // false

delete global_novar; // true

delete global_fromfunc; // true

// test the deletion

typeof global_var; //"number"

typeof global_novar; //"undefined"

typeofglobal_fromfunc; // "undefined"

?

var global =(function () {

return this;

}());

?

热点排行