1.
全局变量:
myglobal =
"hello"; // antipattern
console.log(myglobal); //
"hello"
console.log(window.myglobal); //
"hello"
console.log(window["myglobal"]);
// "hello"
console.log(this.myglobal); //
"hello"
2.
result
为一个隐含的全局变量
function sum(x, y) {
// antipattern: implied global
result = x + y;
return result;
}
修改方法:把
result
使用
var
变成函数范围内局部变量
function sum(x, y) {
var result = x + y;
return result;
}
3.
链式分配
作为var
声名的一部分
// antipattern, do not use
function foo() {
var a = b = 0;
// ...
}
4.
先声名局部变量,链式分配这些局部变量变成全局变量:
function
foo() {
var a, b;
// ...
a = b = 0;
// both local
}
5.
使用
var
声名的全局变量无法使用
delete
删除
// define
three globals
var
global_var = 1;
global_novar
= 2; // antipattern
(function ()
{
global_fromfunc
= 3; // antipattern
}());
// attempt to delete
delete
global_var; // false
delete global_novar; // true
delete global_fromfunc; // true
// test the deletion
typeof global_var; //
"number"
typeof global_novar; //
"undefined"
typeof
global_fromfunc; // "undefined"
6.
访问全局对象:
var global =
(function () {
return this;
}());
分享到:
相关推荐
JavaScript.Patterns
JavaScript Patterns 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
What's the best approach for developing an application with JavaScript? This book helps you answer that question with numerous JavaScript coding patterns and best practices.
more efficient, and easier to work with alone or as part of a team., With Pro JavaScript Design Patterns, you’ll start with the basics of object-oriented programming in JavaScript applicable to ...
With Learning JavaScript Design Patterns, you’ll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to...
True PDF Write reliable code to create powerful applications by mastering advanced JavaScript design patterns
才华横溢的Stoyan Stefanov,他写的由O’Reilly初版的新书《JavaScript Patterns》(JavaScript模式).pdf格式,3M。
Learning JavaScript Design Patterns 英文版,需要的可以看看
https://addyosmani.com/resources/essentialjsdesignpatterns/book/ 2016年5月2日保存。
Mastering JavaScript Design Patterns
Javascript Design Patterns
This book helps you answer that question with numerous JavaScript coding patterns and best practices. If you're an experienced developer looking to solve problems related to objects, functions, ...
(JavaScript.Patterns).Stoyan.Stefanov.文字版
JavaScript Patterns 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn...
javascript设计模式 英文版 pdf
it contains the advanced concept and technology included the object oriented javascript