Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
S6 中引入了箭頭函數,這也是現在前端面試幾乎必考的內容(沒考箭頭函數,我都不好意思說自己是面試官,哈哈,開個玩笑)。有人問我,箭頭函數是個什么東西?我跟他說,就像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可以被當做一個函數來使用。
那么用于承接這個匿名方法的變量實際是什么?
JavaScript: 就是一個js中的function
Java: 在例子中,有點容易迷惑,明明是將lambda賦值給了一個接口類型。但最終調用的時候又要調用該接口的doSomething方法。而且這個接口只能有一個對應的方法,多了會報錯。
Java10中也提供了var關鍵字,但遺憾的是也不能被用于這樣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語言中我們會用一個指向函數的指針。
在上一節的例子中,“hello world”是以參數的形式傳遞到方法中的,那么,是否可以直接引用外部的方法呢?
當然是可以的,改造一下上面的例子:
let str = ",圣誕快樂。";
let func = (s)=> {
console.log(s + str);
str = ",春節快樂。"
};
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 = ",春節快樂。";
};
//str = ",元旦快樂。"
func.doSomething("hello world");
}
var str = ",圣誕快樂。";
var func = (string s) => {
Console.WriteLine(s + str );
str = ",春節快樂。";
};
str = ",元旦快樂。";
func("hello world");
func("hello world");
hello world,元旦快樂。
hello world,春節快樂。
可見,在函數執行的時候,會取當時str的值。在函數定義的時候,雖然引用了變量str,但不是此時固定了str的值。
在函數中改變了str的值,會改變外部str的值。
Java的例子中,要求str是final的才行,所以是無法對str改變的。
在JavaScript中,經常會用到類似callback的回調方法,那么箭頭函數是不是也可以呢?
let func = (s)=> {
console.log(s);
};
var showLog = function(str,action){
action(str);
}
showLog("hello world",func);
本例用Consumer代替了第一節中的自定義的Operate接口。其實Consumer就是框架幫我們預定義的泛型接口,避免我們總需自定義一個接口:
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代替了第一節中的自定義的delegate。其實Action就是框架幫我們預定義的泛型接口,避免我們總需自定義委托:
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的原因?
其實對于面向對象語言來說,好多都是相通的,個人感覺經常對比一下,有助于加深記憶。
另外,如果有機會,學一門風格和自己擅長的開發語言差異比較大的,更有利于對編程語言的了解。
————————————————
版權聲明:本文為CSDN博主「FlyLolo」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/Lolo_cs_dn/article/details/122159246
箭頭函數是在ES6中引入的。但箭頭函數不同于普通函數,箭頭函數提供了一種更為簡潔的語法形式。并且箭頭函數是沒有自己的this,它所謂的 this 是捕獲其所在上下?的 this 值,作為??的 this 值。而普通函數中的this指向是在運行時基于函數的執行環境綁定的,也就是動態的。
首先我們了解一下new一個對象時會發生什么
所以,上面的第二、三步,箭頭函數都是沒有辦法執行的。
#如何學習編程##編程語言##前端##web前端面試題#
點贊關注加收藏,每天都會更新一個JavaScript知識!!!!!!!!!
*請認真填寫需求信息,我們會在24小時內與您取得聯系。