函式回傳值
return可以改變函式內部的"回傳值". 1.一個函式執行完之后,默認的回傳值是undefined,function x(){ return 111; } //函式自執行,內部運行return就是函式的回傳值 console.log( x() );輸出到頁面中:document.write
function sum(a,b){ return a + b; } //實參傳入形參,列印公式回傳值 console.log( sum(4, 5) ** 2 );2.函式執行到return,函式就立即結束,不再往后執行. 3.起作用離他最近的函式,后面代碼不執行
function x( a ){ if (a<10){ return a; } return a * 2; } console.log( x(9) ); console.log( x(20) ); console.log( x(30) );
使用有名函式作為頁面事件點擊回傳值時,只需用函式名賦值
//函式名賦值 let a = 0; function fn(){ a ++; console.log(a); } document.onclick = fn; //函式名自執行 let a = 10 function fn(){ return function(){ a ++; console.log(a); } } document.onclick = fn();
函式中各種this指向
//全域作用域中,this指向window console.log(this);//window function x(){ console.log(this); } //變數函式自執行,this指向window x();//window //函式運算式自執行,this指向window (function(){ console.log(this);//window })(); //事件函式的this指向事件對應的節點物件 /*document.onclick = function(){ console.log(this); };*/ let obj = { name : "Fly", age : 18, say : function(){ console.log(this); } }; //物件的方法自執行時,this指向該物件 obj.say(); //但是如果用來充當事件函式,this還是按照事件函式的規則,指向節點物件 document.onclick = obj.say;
修改this的指向
//call , apply 都是自執行 let obj = { n : "Fly", a : 16 }; function fn( a,b ){ console.log(a+b,this); } // fn();//window //傳入的第一個實參,往后依次傳入的形參,代表this指向 fn.call( obj,2,3 ); //傳入一個陣列里,依次對應形參 fn.apply( obj,[3,4] ); //bind不會主動執行,被動執行 let obj = {a:1,b:2}; let fn = function(){ console.log(this); }; //bind不會主動執行,被動執行 document.onclick = fn.bind(obj);
基礎資料型別:堆疊記憶體, 復雜資料型別:堆記憶體
/*let a = 10; let b = 10; console.log(a === b); //true let c = {n:1}; let d = {n:1}; console.log(c === d);*///false let e = {h:10}; let f = e; //參考關系 // console.log(e === f);//true f.g = 20; console.log(e);
typeof 用來檢測 資料的 資料型別
console.log( typeof 10 ); //"number" console.log( typeof "10" ); //"string" console.log( typeof true ); //"boolean" console.log( typeof undefined ); //"undefined" console.log( typeof {} ); //"object" console.log( typeof [] ); //"object" console.log( typeof window ); //"object" //比較特殊的 console.log( typeof null ); //"object" console.log( typeof function(){} ); //"function" console.log(typeof typeof 10);//"string"
alert的各種彈窗
// alert( 10 ); // alert( "哈哈哈" ); // alert( true ); // alert( undefined ) // alert( null ) // alert( [1,2,3] ); // 陣列的內容1,2,3 // alert( {a:10} ); // 物件變成字串[object Object] //函式彈窗,寫的什么樣就是什么樣 function fn(){ let a = 10; let b = 20; return a+b; } alert( fn );
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/247126.html
標籤:其他
