S6 中引入了箭頭函數(shù),這也是現(xiàn)在前端面試幾乎必考的內(nèi)容(沒考箭頭函數(shù),我都不好意思說自己是面試官,哈哈,開個玩笑)。有人問我,箭頭函數(shù)是個什么東西?我跟他說,就像Java和C#中的lambda。
let func = (s)=> { console.log(s); };
func("hello world");
interface Operate {
void doSomething(String str);
// void doSomething1(); 不可以有兩個方法
}
public static void main(String[] args) {
Operate func = (String s)->{ System.out.println(s);};
func.doSomething("hello world");
}
var func = (string s)=> { Console.WriteLine(s); };
func("hello world");
可以看到,寫法非常類似,尤其是Js和C#。 變量func可以被當(dāng)做一個函數(shù)來使用。
那么用于承接這個匿名方法的變量實際是什么?
JavaScript: 就是一個js中的function
Java: 在例子中,有點容易迷惑,明明是將lambda賦值給了一個接口類型。但最終調(diào)用的時候又要調(diào)用該接口的doSomething方法。而且這個接口只能有一個對應(yīng)的方法,多了會報錯。
Java10中也提供了var關(guān)鍵字,但遺憾的是也不能被用于這樣lambda賦值的情況。
C#: 實際上是一個委托類型,例如:
delegate void doSomething(string str);
public static void Main(string[] args) {
doSomething func = (string s) => { Console.WriteLine(s); };
func("hello world");
}
這樣看和Java有點像了,但定義的仍然是一個方法,而不是一個接口中有一個同樣類型的方法。
如果在c語言中我們會用一個指向函數(shù)的指針。
在上一節(jié)的例子中,“hello world”是以參數(shù)的形式傳遞到方法中的,那么,是否可以直接引用外部的方法呢?
當(dāng)然是可以的,改造一下上面的例子:
let str = ",圣誕快樂。";
let func = (s)=> {
console.log(s + str);
str = ",春節(jié)快樂。"
};
str = ",元旦快樂。"
func("hello world");
func("hello world");
interface Operate {
void doSomething(String str);
// void doSomething1(); 不可以有兩個方法
}
public static void main(String[] args) {
final String str = ",圣誕快樂";
Operate func = (String s)->{
System.out.println(s + str);
//str = ",春節(jié)快樂。";
};
//str = ",元旦快樂。"
func.doSomething("hello world");
}
var str = ",圣誕快樂。";
var func = (string s) => {
Console.WriteLine(s + str );
str = ",春節(jié)快樂。";
};
str = ",元旦快樂。";
func("hello world");
func("hello world");
hello world,元旦快樂。
hello world,春節(jié)快樂。
可見,在函數(shù)執(zhí)行的時候,會取當(dāng)時str的值。在函數(shù)定義的時候,雖然引用了變量str,但不是此時固定了str的值。
在函數(shù)中改變了str的值,會改變外部str的值。
Java的例子中,要求str是final的才行,所以是無法對str改變的。
在JavaScript中,經(jīng)常會用到類似callback的回調(diào)方法,那么箭頭函數(shù)是不是也可以呢?
let func = (s)=> {
console.log(s);
};
var showLog = function(str,action){
action(str);
}
showLog("hello world",func);
本例用Consumer代替了第一節(jié)中的自定義的Operate接口。其實Consumer就是框架幫我們預(yù)定義的泛型接口,避免我們總需自定義一個接口:
public static void main(String[] args) {
Consumer<String> func = (String s)->{
System.out.println(s);
};
showLog("hello world",func);
}
public static void showLog(String str, Consumer<String> action){
action.accept(str);
}
本例用Action代替了第一節(jié)中的自定義的delegate。其實Action就是框架幫我們預(yù)定義的泛型接口,避免我們總需自定義委托:
public static void Main(string[] args)
{
var func = (string s) => { Console.WriteLine(s); };
showLog("hello world", func);
}
public static void showLog(string str ,Action<string> action)
{
action(str);
}
總體來說,三種語言的使用方法還是比較類似的。可能是都源于C的原因?
其實對于面向?qū)ο笳Z言來說,好多都是相通的,個人感覺經(jīng)常對比一下,有助于加深記憶。
另外,如果有機(jī)會,學(xué)一門風(fēng)格和自己擅長的開發(fā)語言差異比較大的,更有利于對編程語言的了解。
————————————————
版權(quán)聲明:本文為CSDN博主「FlyLolo」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/Lolo_cs_dn/article/details/122159246
箭頭函數(shù)是在ES6中引入的。但箭頭函數(shù)不同于普通函數(shù),箭頭函數(shù)提供了一種更為簡潔的語法形式。并且箭頭函數(shù)是沒有自己的this,它所謂的 this 是捕獲其所在上下?的 this 值,作為??的 this 值。而普通函數(shù)中的this指向是在運行時基于函數(shù)的執(zhí)行環(huán)境綁定的,也就是動態(tài)的。
首先我們了解一下new一個對象時會發(fā)生什么
所以,上面的第二、三步,箭頭函數(shù)都是沒有辦法執(zhí)行的。
#如何學(xué)習(xí)編程##編程語言##前端##web前端面試題#
點贊關(guān)注加收藏,每天都會更新一個JavaScript知識!!!!!!!!!
*請認(rèn)真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。