关于调用JavaScript全局对象的方法和属性。The window object is a global object, which means you don’t
关于调用JavaScript全局对象的方法和属性。 The window object is a global object, which means you don’t need to use its name to access its properties and methods. In fact, the global functions and variables (the ones accessible to script anywhere in a page) are all created as properties of the global object. For example, the alert() function you have been using since the beginning of the book is, in fact, the alert() method of the window object. Although you have been using this simply as this: alert(“Hello!”); You could write this with the same, exact results: window.alert(“Hello!”); However, since the window object is the global object, it is perfectly correct to use the first version. window是全局对象,调用它的方法和属性就不用写window.了,怎么理解? 其他面向对象的语言可不是这样的。 JavaScript 对象 [解决办法] (function(){ var alert = function(msg){ window.alert('kk:'+msg) } alert(1); window.alert(1); })(); 这样能明白吧 js有查找顺序的[解决办法] js中有个global对象,是隐藏的,一切全局的都是他的属性,也即是window的属性 可以这样 global.pro; 不过全局的global要省略[解决办法]
引用: js中有个global对象,是隐藏的,一切全局的都是他的属性,也即是window的属性 可以这样 global.pro; 不过全局的global要省略 内容来自javascript高级程序设计第二版
[解决办法] js 就是规定了
默认情况会自动在 window下查找
所以写很短小的代码的时候
大家都省略了 window 反正系统会帮你找的
但是好的习惯 你应该写 window.alert 显式调用
很简单的规则 了解就可以了
[解决办法] <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 6: Example 1</title>
<script type=”text/javaScript”>
window.defaultStatus = “Hello and Welcome”;
for(var i in window) {
if(i == defaultStatus) alert(window[i]);
}
</script>
</head>
<body>
</body>
</html>
[解决办法] 引用: 难道我写的js代码都是在window这个类里面? 是可以这样理解,js的一切都在一个map(object)里。
------解决方案--------------------
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN[color=#FF0000]”[/color]
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 6: Example 1</title>
<script type=”text/javaScript”>
window.defaultStatus = “Hello and Welcome”;
</script>
</head>
<body>
</body>
</html>
你不觉得这些引号看起来怪怪的吗
[解决办法] 引用: The window object is a global object, which means you don’t need to use its name to access its properties and methods. In fact, the global functions and variables (the ones accessible to script anywhere in a page) are all created as properties of the global object. For example, the alert() function you have been using since the beginning of the book is, in fact, the alert() method of the window object. Although you have been using this simply as this: alert(“Hello!”); You could write this with the same, exact results: window.alert(“Hello!”); However, since the window object is the global object, it is perfectly correct to use the first version. window是全局对象,调用它的方法和属性就不用写window.了,怎么理解? 其他面向对象的语言可不是这样的。 说白了就相当于在整个脚本外面加个with(window){}
[解决办法] 引用: Quote: 引用: <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN[color=#FF0000]”[/color] “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml”> <head> <title>Chapter 6: Example 1</title> <script type=”text/javaScript”> window.defaultStatus = “Hello and Welcome”; </script> </head> <body> </body> </html> 你不觉得这些引号看起来怪怪的吗 嗯 我发现了 但是改了以后还是这个错误 defaultStatus不能显示出来你只是给它赋值,当然不会显示了。
window.defaultStatus = "Hello and Welcome";
alert(defaultStatus);
[解决办法]