2-9 Global environment and Global object
- Global objects(Ex: window, on browser) and “this” is created by javascript engine
- At globle level, ‘this’ is equal to ‘window’ inside browser
- the global (values and functions) are attached to global objects
1 |
|
1 |
|
1 |
|
後記:
我到現在才知道在js裡,new後面的是一個function…囧
.prototype (property) is used only by functions
.property is the prototype of any object created(use ‘new’)
__proto__ is in all objects(includeing functions)
__proto__ is the prototype of the the object
.prototype 是改變所有new 出來的新object 的 prototype
看下面例子,john.__proto__ 指向 Person.prototype
∵ methods(function) 也是object,放在function contstrctor,變成每個new 出來的都有一份 methods 的copy
但它們只做一樣的事情 -> 浪費記憶體
1 |
|
if we invoked a function constructor without new , the function is still run. However, the function will return undefined in default.
use capital letter for function constructor
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
if browser doesn’t support Object.create… Polyfill!!
1 |
|
1 |
|
#5-53 Classical vs prototypal ingeritance
#5-54 Understanding Prototype
1 |
|
#5-55 Every things is an object(or a primitive)
1 |
|
#5-56 Reflection and Extend
1 |
|
1 | // require underscore |
1 |
|
Write a program to determine your system’s byte ordering. Explain how does it work and present your test results.
Write a client program and a server program to continuously report the number of processes currently running on a specified host (UNIX) computer. Make sure your server supports multiple concurrent clients and handle socket-related exceptions.
1 |
|
creating a copy of a function but with some preset parameters.
The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.REF
bind 複製function,並將其中的 this 設成給定的物件,回傳複製的function
call 將function中的this改為thisArg, 並用後面的參數invoked 該function
apply和call功能相同,差別只有function的參數要用array傳入
1 |
|
1 |
|
When is this used in real life?
1 |
|
1 |
|
why whattosay is still exist when invoking sayHi ?
greet function is pop off the execution stack, so whattosay should be clear.
Closures: closing in all variables that the function supposed to have access to
the execution context has closed in its outer environment reference(only variables, not values), even though those outer execution contexts are gone.
1 |
|
Closures close only the variables, not the values.
1 |
|
to execute the functions to get different execution context that contains different J s.
1 | function buildFunctions(){ |
ES6 solution
1 |
|
1 |
|
1 |
|
function 被宣告後馬上被執行,回傳hello字串給greeting
1 |
|
syntax parse saw function keyword in the starting in a line, it expects that this is a function statement
1 |
|
因為是呼叫function,有自己的 execution context
所有function 不會動到 global
如果真的需要global的內容,下面給了一個範例
能夠安全的access global content
1 |
|
author.bio
author.job