mynote
==
左邊是int type右邊是string type,但是因為兩個等於會把 string 轉型為int,所以實際比對是
1 = 1
所以是true。
===
如果是三個等於(===),那麼就不會有轉換型別的問題,因此
1 === "1" //false
就是false。
執行JS 後台會執行的事情
JS 後台會執行的事情
What is Hoisting??
function 或 var 在creation phase會先分配給memory
所以先執行再宣告也不會報錯
satHi() //Hi
function sayHi(){
console.log("Hi")
}
function expression(arrow function), const, let都不支援Hoisting,
所以不能先執行再宣告
const x // syntax error
let y // undefined
var z //undefined
const x = 1
const x = 2 // syntax error
let y = 1
let y = 2 // syntax error
var z = 1
var z = 2 // ok!
const x = 1
x = 2 // syntax error
let y = 1
y = 2 // ok!
var z = 1
z = 2 // ok!
if(true){
var x=1;
}
conole.log(x) //1
function 的變數取決於function被定義的scope
是一種資料結構, 遵循last in first out
All functions are objects
製作大量相似的obejects
function Person(name, age){
this.name = name;
this.age = age;
this.hi = function(){
console.log("Hi")
}
}
let Mike = new Person("Mike", 33)
let Lex = new Person("Lex", 38)
Mike.hi() // Hi
如果有共有的function或是property, 可以使用prototype來指向同一個memory位置, 避免memory浪費
function Person(name, age){
this.name = name;
this.age = age;
}
Person.prototype.hi = function(){
console.log("Hi")
}
let Mike = new Person("Mike", 33)
let Lex = new Person("Lex", 38)
Mike.hi() // Hi
Prototype is a simple reference to another object; this object contains common properties and methods across all instances.
string的兩種創建方式
myName = "Dennis"
myName = new String("Dennis")
用constructor創造的string就會繼承prototype
但是沒必要這樣做, myName = "Dennis"即可
要使用的時候內建function時候
會自動轉換myName = new String("Dennis")
funtion 內部的this是對應到window而不是function本身
bind
可以將function本身丟回去, 但是要賦予新的變數
call
可以將function本身丟回去, 不用賦予新的變數
也可以傳參數
apply
和call一樣的用法, 只是參數要用list
用來繼承prototype
function Foo(name) {
this.name = name;
}
var b = new Foo('b');
var a = new Foo('a');
b.say = function() {
console.log('Hi from ' + this.whoAmI());
}
console.log(a.__proto__ === Foo.prototype); // true
console.log(a.__proto__ === b.__proto__); // true
//Function的原型对象
console.log(1,Function.prototype);
//Object的原型对象
console.log(2,Object.prototype);
//Function的原型对象的原型对象
console.log(3,Function.prototype.__proto__);
console.log(4,Object.prototype === Function.prototype.__proto__);
let sayHi = ()=>{}
feature
创建一个新的数组,其中每一个元素由调用数组中的每一个元素执行提供的函数得来。
let arr =[1,2,3,4,5,6]
let list = arr.map(value => {
return value * value;
});
//list = [1,4,9,16,25,36]
针对每一个元素执行提供的函数。
let arr =[1,2,3,4,5,6]
arr.forEach((value, key) => {
return arr[key] = value * value;
});
//[1,4,9,16,25,36]
一般同步的狀況
一般同步的狀況2的等待時間比較短會先打印
window.setTimeout(() => {console.log(1)},2000)
window.setTimeout(() => {console.log(2)},1000)
//2
//1
callback的狀況
function delay(message, time) {
return new Promise(function (resolve, reject) {
window.setTimeout(() => {resolve(`${message}`)}, time)});
}
p = delay;
p(1,2000).then((a) => {
console.log(a);
return delay(2, 1000);
}).then(b=>console.log(b))
//1
//2
Promise的狀況
function delay(message, time) {
return new Promise(function (resolve, reject) {
window.setTimeout(() => {resolve(`${message}`)}, time)});
}
p = delay;
p(1,2000).then((a) => {
console.log(a);
return delay(2, 1000);
}).then(b=>console.log(b))
//1
//2
Promise用then接正確的結果, 用catch接錯誤的結果
function delay(message, time) {
return new Promise(function (resolve, reject) {
window.setTimeout(() => {resolve(`${message}`)}, time)});
}
p = delay;
p(1,2000).then((a) => { //正確的結果顯示這裡
console.log(a);
return delay(2, 1000);
}).then(b=>console.log(b))
.catch((err) => { //錯誤的結果顯示這裡
console.log("err", err);
});
//1
//2
ASYNC的狀況
function delay(message, time) {
return new Promise(function (resolve, reject) {
window.setTimeout(() => {resolve(`${message}`)}, time)});
}
async function test(){
console.log(await delay(1,2000))
console.log(await delay(2,1000))
}
test()
AJAX = Asynchronous JavaScript And XML
Q: Why AJAX?
A: connect with API
promise
fetch(requestURL)
.then((response) => {
return response.json();
})
.then((data) => {
console.log(`Title: ${data.title}`);
if (data.completed !== true){
console.log("Undo");
} else {
console.log("Done!!");
}
})
.catch((error) => {
console.log(`Error: ${error}`);
})
async
const Find = async () => {
const dataFetch = await fetch(
"https://dennis89linebot.herokuapp.com/myNote",
{
method: "GET",
headers: {
Accept: "application/json",
},
}
);
let parseData = await dataFetch.json();
setDatas(parseData);
console.log("first", parseData);
};
fetch如果不用async or promise
下面的code會先執行,導致沒有讀到值
const dataFetch1 = fetch(
"https://dennis89linebot.herokuapp.com/myNote",
{
method: "GET",
headers: {
Accept: "application/json",
},
}
);
let parseData1 = dataFetch1.json();
https://ithelp.ithome.com.tw/articles/10268821