TIL coding
  • Initial page
  • 排版
  • Flexbox
  • Grid
  • jQuery
  • Untitled
  • JavaScript
    • An Introduction to JavaScript
    • Hello, world!
    • Code structure
    • The modern mode, "use strict"
    • Variables
    • Data types
    • Type Conversions
    • Operators
    • Comparisons
    • Interaction: alert, prompt, confirm
    • Conditional operators: if, '?'
    • Logical operators
    • Loops: while and for
    • The "switch" statement
    • Functions
    • Function expressions and arrows
    • JavaScript specials
    • Comments
    • Ninja code
    • Automated testing with mocha
    • Polyfills
    • Objects
    • Garbage collection
    • Symbol type
    • Object methods, "this"
    • Object to primitive conversion
    • Constructor, operator "new"
    • Methods of primitives
    • Numbers
    • Strings
    • Arrays
    • Array methods
    • Iterables
    • Map, Set, WeakMap and WeakSet
    • Object.keys, values, entries
    • Destructuring assignment
    • Date and time
    • JSON methods, toJSON
    • Recursion and stack
    • Rest parameters and spread operator
    • Closure
    • The old "var"
    • Global object
    • Function object, NFE
    • The "new Function" syntax
    • Scheduling: setTimeout and setInterval
    • Decorators and forwarding, call/apply
    • Function binding
    • Currying and partials
    • Arrow functions revisited
    • Property flags and descriptors
    • Property getters and setters
    • Prototypal inheritance
    • F.prototype
    • Native prototypes
    • Prototype methods, objects without __proto__
    • The “class” syntax
    • Class inheritance
    • Static properties and methods
    • Private and protected properties and methods
    • Extending built-in classes
    • Class checking: "instanceof"
    • Mixins
    • Error handling, "try..catch"
    • Custom errors, extending Error
    • Introduction: callbacks
    • Promise
    • Promises chaining
    • Error handling with promises
    • Promise API
  • Bootstrap
    • Navbar
Powered by GitBook
On this page

Was this helpful?

  1. JavaScript

Global object

在網頁叫 window,在 node.js 叫 global。

// window 除了全域物件也可以做為視窗的物件
alert(window.innerHeight); // 显示浏览器窗口高度
window.open('http://google.com'); // 打开一个新的浏览器窗口
var x = 5;

// var 自動變成 window 屬性
alert(window.x); // 5 (变量 x 成为 window 的一个属性)
window.x = 0;
alert(x); // 0, 变量已修改

// let、const 不會
let x = 5;
alert(window.x); // undefined ("let" 不会创建窗口属性)

// 全域變數在不同腳本可以共用
<script>
  var a = 1;
  let b = 2;
</script>

<script>
  alert(a); // 1
  alert(b); // 2
</script>

// 全域 this 的值是 window
alert(this); // window

// 加上 module 腳本會有自己的作用域
<script type="module">
  var x = 5;
  alert(window.x); // undefined
</script>

// 不同腳本無法共用
<script type="module">
  let x = 5;
</script>

<script type="module">
  alert(window.x); // undefined
  alert(x); // 错误:未声明的变量
</script>

// this 的值不是 window
<script type="module">
  alert(this); // undefined
</script>

// 通常會減少全域變數,但若真有需要可能希望放到全域物件中
// 明确地将它分配给 `window`
window.currentUser = {
  name: "John",
  age: 30
};

// 然后,在另一个脚本中
alert(window.currentUser.name); // John

// 測試瀏覽器是否支持新功能
if (!window.Promise) {
  alert("Your browser is really old!");
}

// 自訂義新功能
if (!window.Promise) {
  window.Promise = ... // 自定义实现现代语言特性
}

PreviousThe old "var"NextFunction object, NFE

Last updated 5 years ago

Was this helpful?