LearnJs是一種嘗試描繪JavaScript的最佳部分,這些部分非常困難且難以找到。可以指出的是,這不是任何形式的書/指南,而是最佳實踐,語言構造和其他簡單但有效的片段的會眾,這使我們能夠使我們如何利用最好的語言。
1.1 Declarations :
// bad
var arr = new Array ( ) ;
var str = new String ( ) ;
var num = new Number ( ) ;
var boo = new Boolean ( ) ;
var obj = new Object ( ) ;
var reg = new RegExp ( ) ;
var fun = new function ( ) ;
// good
var arr = [ ] ,
str = "" ,
num = 0 ,
boo = false ,
obj = { } ,
reg = / () / ,
fun = function ( ) { } ;我們必須了解以下事實:在JavaScript中,所有內容都是一個對象,因此,如果我們使用String對象聲明字符串並將其與var a = ""進行比較,那麼比較的結果將是錯誤的。這僅僅是因為如果我們使用bad方式聲明字符串並將其與使用good方法聲明的字符串進行比較,那麼從根本上講,我們將字符串與對象(字符串)進行比較。
Semicolons :
// Snippet one
var foo = { }
foo . code = "this is javascript empire"
foo . engine = "node 0.12.7"
foo . author = "akhil pandey"
foo . version = 0.1
// Snippet two
var bar = { } ;
bar . name = "akhil pandey" ;
bar . url = "www.akhilhector.com" ;
bar . github = "AkhilHector" ;
bar . age = 20 ;
if ( typeof ( bar ) == typeof ( foo ) ) {
console . log ( "Semicolons donot matter at all" )
}代碼片段一和兩個是相同的。但是,兩個代碼樣本之間的基本差異是,一個人在lang -uage語義中使用了半殖民者,而另一個則沒有。基本上,我們被教導使用諸如C,C ++,Java等語言中的半殖民物,因為使用';'終止代碼行終止。但是在JavaScript中,整個情況都不同。在有或沒有半糖的情況下,執行代碼絕對沒有差異。
方法的一部分JavaScript地圖對象:
2.1 map .set():
let m1 = new Map ( ) ;
let x = { id : 1 } ,
y = { id : 2 } ;
m1 . set ( x , "foo" ) ; // Map { { id: 1 } => 'foo' }
m1 . set ( y , "bar" ) ; // Map { { id: 1 } => 'foo', { id: 2 } => 'bar' }
m1 . set ( "name" , "akhil pandey" ) //Map { { id: 1 } => 'foo', { id: 2 } => 'bar', 'name' => 'akhil pandey' } map .set()是用於添加或更新圖中具有特定鍵和值的元素的方法。在這裡,第一個參數是關鍵,而第二個參數是值。這些鍵可以是任何類型的,但是最好將objects用作鍵而不是strings ,因為如果我們將strings用作鍵, Maps和Objects之間的差異不會有任何顯著差異。
2.2地圖.get():
var m1 = new Map ( ) ;
let x = { id : 1 } ,
y = { id : 2 } ;
m1 . set ( x , "foo" ) ; // Map { { id: 1 } => 'foo' }
m1 . set ( y , "bar" ) ; // Map { { id: 1 } => 'foo', { id: 2 } => 'bar' }
m1 . get ( x ) ; //returns 'foo' map .get()是一種用特定鍵從Map對象檢索元素的方法。因此,鍵是作為參數傳遞的,並且與該密鑰關聯的元素返回。如果沒有作為參數傳遞的鍵,則該方法將以undefined方式返回。
2.3地圖.has():
var m1 = new Map ( ) ;
let x = { id : 1 } ,
y = { id : 2 } ;
m1 . set ( x , "foo" ) ; // Map { { id: 1 } => 'foo' }
m1 . set ( y , "bar" ) ; // Map { { id: 1 } => 'foo', { id: 2 } => 'bar' }
m1 . has ( x ) // returns true
m1 . has ( "akhil" ) // retuqrns false map .has()是一種方法,該方法指示圖中是否存在帶有請求密鑰的元素。該方法僅採用一個參數,即鍵,如果元素不存在,則返回true false如果元素不存在。
2.4地圖。大小:
var m1 = new Map ( ) ;
let x = { id : 1 } ,
y = { id : 2 } ;
m1 . set ( x , "foo" ) ; // Map { { id: 1 } => 'foo' }
m1 . set ( y , "bar" ) ; // Map { { id: 1 } => 'foo', { id: 2 } => 'bar' }
m1 . size ; MAP .Size是一種訪問屬性,可返回Map對像中存在的元素數量。由於它是一個訪問屬性,我們不應該像一種方法一樣調用/使用此功能,因此,如果調用m1.size()則它會拋出一個typeError,說m1.size不是函數。因此,對該屬性的有效呼叫為m1.size 。
2.5 MAP .CLEAR():
var m1 = new Map ( ) ;
let x = { id : 1 } ,
y = { id : 2 } ;
m1 . set ( x , "foo" ) ; // Map { { id: 1 } => 'foo' }
m1 . set ( y , "bar" ) ; // Map { { id: 1 } => 'foo', { id: 2 } => 'bar' }
m1 . has ( x ) ; // returns true
m1 . clear ( ) ;
m1 . has ( x ) ; // returns false MAP .CLEAR()是一種清除/刪除Map對像中存在的所有元素的方法。該方法不會採取任何參數,而是為了回報而拋出undefined 。
2.6地圖.delete():
var m1 = new Map ( ) ;
let x = { id : 1 } ,
y = { id : 2 } ;
m1 . set ( x , "foo" ) ; // Map { { id: 1 } => 'foo' }
m1 . set ( y , "bar" ) ; // Map { { id: 1 } => 'foo', { id: 2 } => 'bar' }
m1 . has ( x ) ; // returns true
m1 . delete ( x ) ; // returns true
m1 . delete ( "something" ) ; // returns false
m1 . has ( x ) ; // returns false map .delete()是用於從Map對象刪除特定元素的方法。該方法僅採用一個參數,即鍵,如果Map中存在鍵,它將刪除元素並返回一個true ,但是如果鍵在Map中不存在,則會拋出一個false 。
2.7地圖.keys():
var m1 = new Map ( ) ;
let x = { id : 1 } ,
y = { id : 2 } ;
m1 . set ( x , "foo" ) ; // Map { { id: 1 } => 'foo' }
m1 . set ( y , "bar" ) ; // Map { { id: 1 } => 'foo', { id: 2 } => 'bar' }
m1 . keys ( ) ; // MapIterator { { id: 1 }, { id: 2 } }
let iterator = m1 . keys ( ) ;
console . log ( iterator . next ( ) . value ) ; // { id: 1 }
console . log ( iterator . next ( ) . value ) ; // { id: 2 }
console . log ( iterator . next ( ) . value ) ; // undefined map .keys()是一種用於返回每個元素中Map對像中存在的鍵。該方法返回一個地圖迭代對象,該對象可用於了解Map中存在的密鑰。在上面的示例中,它概述瞭如何使用next()在map .keys()上迭代。應該注意的是,當在迭代對像上使用next()時,必須使用一個value或done ,因為next()的直接使用導致顯示Object 。 iterator.next( true iterator.next().done iterator.next().value返回迭代對像中存在的鍵的值false
2.8 MAP .VALUES():
var m1 = new Map ( ) ;
let x = { id : 1 } ,
y = { id : 2 } ;
m1 . set ( x , "foo" ) ; // Map { { id: 1 } => 'foo' }
m1 . set ( y , "bar" ) ; // Map { { id: 1 } => 'foo', { id: 2 } => 'bar' }
m1 . values ( ) ; // MapIterator { 'foo', 'bar' }
let iterator = m1 . values ( ) ;
console . log ( iterator . next ( ) . value ) ; // foo
console . log ( iterator . next ( ) . value ) ; // bar
console . log ( iterator . next ( ) . value ) ; // undefined Map .Values()是一種用於返回每個元素中Map對像中存在的值的方法。該方法返回一個迭代對象,該對象可用於了解Map中存在的值。在上面的示例中,它概述瞭如何使用next()在map .values()上迭代。應該注意的是,當在迭代對像上使用next()時,必須使用一個value或done ,因為next()的直接使用導致顯示Object 。 iterator.next( true iterator.next().done iterator.next().value返回迭代對像中存在的特定元素的值false
2.9地圖.entries():
var m1 = new Map ( ) ;
let x = { id : 1 } ,
y = { id : 2 } ;
m1 . set ( x , "foo" ) ; // Map { { id: 1 } => 'foo' }
m1 . set ( y , "bar" ) ; // Map { { id: 1 } => 'foo', { id: 2 } => 'bar' }
m1 . entries ( ) ; // MapIterator { [ { id: 1 }, 'foo' ], [ { id: 2 }, 'bar' ] }
let iterator = m1 . values ( ) ;
console . log ( iterator . next ( ) . value ) ; // [ { id: 1 }, 'foo' ]
console . log ( iterator . next ( ) . value ) ; // [ { id: 2 }, 'bar' ]
console . log ( iterator . next ( ) . value ) ; // undefined map .entries()是一種方法,該方法被起訴返回每個元素的Map對像中存在的鍵和值。該方法與Map .Values()非常相似,因為它返回了一個可以用來了解Map中存在的鍵和值的迭代對象。在上面的示例中,展示瞭如何使用next()在MAP上迭代。該方法的能力及其操作方式與MAP .VALUES()非常相似,因為當我們通過迭代對象迭代時,鍵也與值一起返回。與任何迭代物體到達平面的末端后,它都會拋出和undefined 。同樣,要注意的是,適用於迭代對象的任何其他方法都可以與此使用,因為它是一個且相同的方法。
MISC[Maps] :弱圖可以視為圖中,在引擎蓋下發生垃圾收集的方法差異。為了簡單明了的術語,必須有一些概念性的課程才能帶來觀點。在JavaScript中,所有事物都廣泛地是一個對象,因此創建一個對象,並且將存儲器分配給相同,直到V8自動GC(垃圾收集的簡短)直到圖中的節點一直保持為節點為止。應當指出的是,直到沒有對內存中創建的對象的引用,它將不會被GC,因此所有對像都被tightly或strongly固定。因此,從本質上講,使用弱圖帶來的區別在於,如果對像是由V8 gc gc gy或您,則該對象的鍵weakly持有,或者該對象將從弱圖中刪除,但不是值。映射和弱映射的操作方式之間沒有明顯的區別,儘管弱圖僅接受對像作為鍵(嚴格)。讓我們在下面看一下它們是如何初始化的:
var m1 = new WeakMap ( ) ;
let x = { id : 1 } ,
y = { id : 2 } ;
m1 . set ( x , "foo" ) ; // WeakMap {}
m1 . set ( y , "bar" ) ; // WeakMap {}
m1 . get ( x ) ; // 'foo'
m1 . get ( y ) ; // 'bar'm1.值得注意的是,儘管弱圖API與地圖對象相同的方式與我們相互作用,但弱圖API提供的操作存在局限性。截至目前,它支持get() , set() , has()和delete()方法。
MISC[Maps] :使用...運算符
方法的一部分JavaScript集對象:
3.1 set .add():
var s1 = new Set ( )
s1 . add ( 'akhil' )
s1 . add ( 123 )
s1 . add ( 456.789 )
s1 . add ( true )
s1 . add ( { id : 123456 } )
s1 . add ( NaN )
s1 . add ( null )
console . log ( s1 ) // Set { 'akhil', 123, 456.789, true, { id: 123456 }, NaN, null} set .add()是一種用於將值添加到集合的突變器方法。實現這一目標的過程很簡單。我們將add()方法稱為已創建的Set對象,並將我們想要的值傳遞到集合中作為參數。當將多個參數傳遞給add()方法時,它只是忽略了其餘的參數,而僅考慮第一個參數。應該注意的是, Set對像對所傳遞的值類型沒有任何限制,也不會在我們製作Set異質時執行任何限制。 NaN , null也可以成為Set的一部分。雖然,從未鼓勵用這種性質的價值填充Set 。
3.2 set .has():
var s1 = new Set ( )
s1 . add ( 123 )
s1 . add ( 456 )
s1 . add ( 789 )
s1 . add ( "akhil" )
s1 . add ( null )
s1 . has ( 123 ) // returns true
s1 . has ( NaN ) // returns false
s1 . has ( "akhil" ) // returns true
s1 . has ( 456.12 ) // returns false
s1 . has ( 123.0000000000000009123 ) // returns true set .has()是一種有助於確定Set中元素是否存在的方法。如果存在值,則該方法將返回true ,如果不存在該值,則返回false 。如果我們觀察到上述示例,則該方法的預期性質與其地面現實之間存在很小的差異。 s1.add(123.0000000000000009123)是一個示例,它闡明了JavaScript和浮點值之間的十年長期競爭。可以簡單地使用數字.toprecision()避免這種情況。另外,在JavaScript中,總是鼓勵仔細處理浮點。
3.3設置.size:
var s1 = new Set ( [ 1 , 23 , 456 , 78910 ] )
var s2 = new Set ( )
s2 . add ( "akhil" )
s2 . add ( "chandu" )
s2 . add ( "adheeth" )
console . log ( s1 . size ) // returns 4
console . log ( s2 . size ) // returns 3 SET .size不是一種方法,而是集合對象的屬性,可用於確定Set的大小。正如您可以從上面的代碼片段中觀察到的那樣,它被稱為訪問方法。如果我們嘗試使用size()來調用該屬性,那麼它會引發一個typeError ,說Set.size不是一個函數。
3.4 set .clear():
var s1 = new Set ( )
s1 . add ( "akhil" )
s1 . add ( "krishna" )
s1 . add ( "pardhu" )
console . log ( s1 . has ( "akhil" ) ) // returns true
console . log ( s1 . has ( "krishna" ) ) // returns true
s1 . clear ( "pardhu" )
s1 . clear ( )
console . log ( s1 . has ( "akhil" ) ) // returns false
console . log ( s1 . has ( "pardhu" ) ) // returns false Set .Clear()是一種清除集合中所有元素的方法。這會導致Set 。如果我們在上面的代碼段中觀察到,則嘗試將參數傳遞給set .clear()方法,而無論將哪些參數傳遞給了它只是忽略的方法,並在Set上執行了clear()操作。同樣,對執行操作的次數沒有限制。
3.5 set .delete():
var s1 = new Set ( )
s1 . add ( "akhil" )
s1 . add ( "dusi" )
s1 . add ( "om" )
s1 . add ( "siddu" )
console . log ( s1 . has ( "akhil" ) )
console . log ( s1 . delete ( "akhil" ) )
console . log ( s1 . delete ( "foo" ) )
console . log ( s1 . has ( "akhil" ) )
for ( let item of s1 ) {
console . log ( item )
} Set .Delete()是一種用於從集合中刪除元素的方法。該方法僅接受一個參數,並返回一個布爾值true或false 。如果將多個參數傳遞給delete()那麼它只是忽略了其餘的參數,而僅考慮第一個參數。
3.6 set .values():
var s1 = new Set ( )
var s2 = new Set ( )
s1 . add ( "foo" )
s1 . add ( "bar" )
s1 . add ( "foobar" )
s1 . add ( "barfoo" )
for ( let items of s1 . values ( ) ) {
s2 . add ( items )
}
console . log ( s1 . values ( ) ) // returns SetIterator { 'foo', 'bar', 'foobar', 'barfoo' }
console . log ( s2 . values ( ) ) // returns SetIterator { 'foo', 'bar', 'foobar', 'barfoo' }
console . log ( s1 . has ( "foo" ) ) // returns true
console . log ( s2 . has ( "foo" ) ) // returns true set .values()是一種返回Set中存在的值的方法。如果我們在循環中使用s1.values()如上面的代碼段所示,那麼當我們在Set上迭代時,我們所獲得的只是一個值。同樣,這聽起來可能與Map中的方法相似,但是有一個較小的區別,即Map .Values()只會返回Map和MAP .KEYS()中存在的值()僅返回與每個值/元素關聯的鍵。現在,由於Set具有相同元素的鍵和值,因此set .values()和set .keys()之間沒有太大的區別。我們將使用相同的峰值對象返回,而完全沒有區別。
3.7設置.entries():
var s1 = new Set ( )
s1 . add ( "foo" )
s1 . add ( "goo" )
s1 . add ( "bar" )
s1 . add ( "gar" )
// the keys are
for ( let pairs of s1 . entries ( ) ) {
console . log ( "key[" + pairs [ 0 ] + "] => " + pairs [ 1 ] )
} Set .Entries()是一種返回Set中存在值的值數組。我們必須了解,在Set中,鍵和值保持不變,因此,當我們調用set .entries()方法時,我們得到的就是NX2值數組,其中n [0]和n [1]包含相同的值。我們還可以將s1.entries()分配到變量或常數,在這種情況下,我們將獲得一個可以使用next()播放的迭代對象。
MISC[Sets] :弱者
MISC[Sets] :使用...運算符
方法的一部分JavaScript數組對象:
4.1 array .push():
var arr1 = [ 1 , 2 , 3 , 4 , 5 ] ;
arr1 . push ( 6 ) ;
console . log ( arr1 ) ; // it prints [1,2,3,4,5,6]Array .push()只是用於在數組中添加元素的突變函數。因此,我們可以簡單地將元素作為函數array.push()的參數提及,並且將作為數組中的最後一個元素添加。
4.2 array .pop()
var arr1 = [ 1 , 2 , 3 , 4 , 5 ] ;
arr1 . pop ( ) ; // removes last element from the array
arr1 . pop ( 23 ) ; // removes last element despite giving the number as argument
arr1 . pop ( "lol" ) ; // removes last element despite giving the string as argument
console . log ( arr1 ) ; // it prints [1,2,3,4]數組.pop()只是一個簡單的突變器函數,用於刪除數組的最後一個元素。因此,該方法沒有參數,儘管如果我們嘗試通過參數,那將不會接受它們。它僅執行刪除數組的最後一個元素的基本操作。
4.3 array .indexof()
var arr1 = [ "akhil" , "chandu" , "adheeth" , "varma" ] ;
var arr2 = [ "akhil" , "chandu" , "adheeth" , "varma" , "akhil" , "shankar" , "akhil" ] ;
arr1 . indexOf ( "akhil" ) ; // returns 0 since akhil is present at index 0
arr1 . indexOf ( "adheeth" ) ; // returns 2 since adheeth is present at index 2
arr2 . indexOf ( "akhil" ) ; // returns 0 because it is the first occurrence of akhilArray .Indexof()是一種訪問器功能,可用於查找數組中特定元素的索引。傳遞給此的論點是數組中元素的值。同樣,要注意的是,當數據中存在多個出現相同元素時,則顯示數組中該元素的第一次出現。
4.4 array .lastIndexof()
var arr1 = [ "akhil" , "chandu" , "adheeth" , "varma" , "akhil" , "shankar" , "akhil" ] ;
arr1 . lastIndexOf ( "akhil" ) ; // returns 6 since akhil last occurred at index 6
arr1 . lastIndexOf ( "adheeth" ) ; // returns 2 since adheeth last occurred at index 2 array .lastIndexof()是一個訪問器函數,行為與indexOf函數非常相似。雖然,差異是lastIndexOf返回數組中元素的最後一次出現的索引。
4.5 array .concat()
var arr1 = [ "akhil" , "chandu" ] ;
var arr2 = [ "adheeth" , "varma" , "kp" ]
var arr3 = [ ]
arr1 . concat ( arr2 ) ; // returns [ 'akhil', 'chandu', 'adheeth', 'varma', 'kp' ]
arr2 . concat ( arr1 ) ; // returns [ 'adheeth', 'varma', 'kp', 'akhil', 'chandu' ]
arr3 = arr1 . concat ( arr2 )
console . log ( arr3 ) // returns [ 'akhil', 'chandu', 'adheeth', 'varma', 'kp' ] Array .concat()是一種訪問器函數,用於從現有數組創建新數組。它以數組作為參數,並且在執行函數後,參數中的數組將與數組調用concat()連接。
4.6數組.splice():
// case 1 :
var arr1 = [ 1 , 2 , 3 , 4 , 5 ] ;
nums = [ 6 , 7 , 8 , 9 , 10 ]
arr1 . splice ( 5 , 0 , nums )
console . log ( arr1 ) ; // it prints [1, 2, 3, 4, 5, [6, 7, 8, 9, 10]]
// case 2 :
var arr2 = [ 'one' ] ;
arr2 . splice ( 1 , 0 , 'two' , 'three' , 'four' , 'five' ) ;
console . log ( arr2 ) ; // it prints ['one', 'two', 'three', 'four', 'five']
// case 3 :
var arr3 = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ;
arr3 . splice ( 5 , 5 ) ;
console . log ( arr3 ) ; // it prints [1, 2, 3, 4, 5]Array .splice()具有可以執行的各種操作。如果我們觀察到案例1,則將從第五索引中添加到數組ARR1的元素。另外,如果我們在案例2中觀察到,我們可以看到,在不分配變量的情況下,要添加的元素將作為連續參數傳遞。要注意的是,剪接不僅可以執行添加元素的動作,而且還可以執行刪除元素的動作。在函數拼接()中,如果我們選擇第二個參數的值為“ 0”,則只會從指定的索引中添加元素,儘管如果值不是“ 0”,則指定的數字將是將要刪除的元素數量。
4.7 array .shift():
var arr1 = [ 1 , 2 , 3 , 4 , 5 , "akhil" , "chandu" , "varma" , "kp" , "adheeth" ] ;
arr1 . shift ( ) ;
arr1 . shift ( 123 ) ;
arr1 . shift ( "lolagain" ) ;
console . log ( arr1 ) ; // it prints [4,5,6,"akhil","chandu","varma","kp","adheeth"]Array .shift()與上述方法Array.pop()沒有什麼不同,儘管主要區別是關於要刪除的元素的索引。它刪除了數組的第一個元素。與array.pop()類似,此方法也不採用參數,即使通過一個參數,它仍繼續執行其操作。
4.8陣列.unshift():
var arr1 = [ 1 , 2 , 3 , 4 , 5 ] ;
str = "akhil pandey" ;
arr1 . unshift ( str ) ;
console . log ( arr1 ) ; // it prints ["akhil pandey", 1, 2, 3, 4, 5];Array .unShift()與Array.push()的類別屬於同一類別,因為它們兩個都用作將元素添加到數組中的突變函數。這兩種方法之間的唯一區別是,如果我們將參數傳遞給array.unshift(),則該元素將作為數組的第一個元素添加。如果我們觀察到上述片段,我們可以看到元素“ 1”的索引移至下一個位置,並且“ Akhil Pandey”是陣列的第一個元素。因此,與array.push()不同,此方法不應僅僅添加元素,因為它會將元素添加到數組的開始。
4.9 array .reverse():
var arr1 = [ 1 , 'akhil' , 'varma' , 'chandu' , 'adheeth' ] ;
arr1 . reverse ( ) ;
console . log ( arr1 ) ; // it prints ['adheeth', 'chandu', 'varma', 'akhil', 1]數組.reverse()只是一個突變函數,用於逆轉數組中元素的順序。
4.10 array .sort():
var arr1 = [ 'varma' , 'chandu' , 'akhil' , 'adheeth' ] ;
var arr2 = [ 5 , 6 , 2 , 9 , 23 ] ;
arr1 . sort ( ) ;
arr2 . sort ( ) ;
console . log ( arr1 ) ; // it prints ['adheeth', 'akhil', 'chandu', 'varma']
console . log ( arr2 ) ; // it prints [2, 5, 6, 9, 23]Array .sort()也是另一個用於將元素按順序放置的突變器函數。可以使用array.sort()方法對字符串和數字進行排序。分類以升序進行,因此,如果字符串或字符是元素,則根據字母順序排列,則分類發生。
4.11 array .map():
function add ( arr ) {
return arr = "My name is " + arr ;
}
var arr1 = [ "akhil" , "varma" , "chandu" , "adheeth" , "kp" ] ;
var combine = arr1 . map ( add ) ;
console . log ( combine ) ;
/* it prints
[ 'My name is akhil',
'My name is varma',
'My name is chandu',
'My name is adheeth',
'My name is kp' ]
*/array .map()是一種更像迭代器函數的方法,但是此和array.foreach()之間的基本差異是array.map()返回帶有結果的新數組,而array.foreach(foreach()並不返回帶有函數結果的新數組。
注意:array.map()是一種非常強大的方法,可以應用於不同的應用程序。儘管此方法使用array.map()在每個元素上迭代,並且必須仔細查看,如果目的是在某些元素上迭代時,則不應使用。
4.14 Array .Reduce():
function combine ( prev , curr ) {
return prev + curr ;
}
var arr1 = [ 1 , 2 , 3 , 4 , 5 ] ;
var arr2 = [ "one " , "two " , "three " , "four " , "five " ] ;
var numsum = arr1 . reduce ( combine ) ;
var worsum = arr2 . reduce ( combine ) ;
console . log ( numsum ) ; // it prints 15
console . log ( worsum ) ; // it prints "one two three four five "Array .Reduce()是一種方法,可以通過將函數作為參數來與數組一起使用,從而使函數在數組元素上迭代。 Array.Reduce()在數組元素上迭代,因此到達陣列的末端后會產生一個值。
注意:array.reduceright()更類似於array.reduce(),但是它在數組元素上從最右邊的元素到最左邊的元素進行了迭代,而不是按照通常的方式進行迭代。
TIPS[Arrays] :
MISC[Arrays] :
通過編寫自己的方法來操縱數組對象:
var boo = [ ]
Array . prototype . foo = function ( ) {
console . log ( "We write our method inside this block" )
}
boo . foo ( ) // returns whatever is included inside the above mentioned code block將方法添加到array.protype本質上意味著我們將方法添加到全局數組對象。因此,Array.Prototype實際上意味著將新的原型添加到現有數組對像中。因此,可以使用以下代碼段來解釋更好的類比。
Array . prototype . union = function ( bar ) {
var l = this . length ;
var n = bar . length ;
for ( i = 0 ; i < n ; ++ i ) {
this [ l ] = bar [ i ] ;
l ++ ;
}
console . log ( this ) ;
}
var a = [ "one" , "two" ] ;
var b = [ "three" , "four" , "five" , "six" , "seven" ] ;
var c = [ 1 , 2 ] ;
var d = [ 3 , 4 , 5 , 6 , 7 ] ;
a . union ( b ) ;
c . union ( d ) ;觀察上面的數組原型,如果我們仔細觀察到它只是陣列方法array.prototype.concat()工作方式的工作複製品。因此,在Concat()方法中,另一個數組作為參數將其傳遞給該方法和主要數組凸進,並擴展了數組。
在上面的示例中要查找的內容是如何編寫自定義方法,以適合特定目的,不僅適合數組原型,還適合所有Javscript識別的對象,例如字符串,數字,regexp或對象本身。
關聯陣列:
最好這是該語言的重要組成部分,儘管這是PHP和Python等許多編程語言中不可或缺的一部分,但它在其他編程語言中提供的內容略有變化。
[注意]:在Python中,它沒有被稱為或稱為關聯數組,但它帶有名稱字典。
var a = [ ] ;
var b = [ ] ;
a [ "one" ] = "boo this is my first item" ;
a [ "two" ] = "foo this is my second item" ;
a [ "three" ] = "alas this is final item" ;
b [ 0 ] = "oh not again the first item" ;
b [ 1 ] = "cant help with the second item" ;
b [ 3 ] = "finally got rid with the third item" ;
console . log ( a ) ; // would display the contents of the array 'a'
console . log ( b ) ; // would display the contents of the array 'b'
var len1 = a . length ;
var len2 = b . length ;
var len3 = Object . keys ( a ) . length ;
console . log ( len1 ) ; // would display undefined
console . log ( len2 ) ; // would display 3
console . log ( len3 ) ; // would display 3以上片段是帶有命名索引或關聯陣列的陣列的經典案例實現。可以如上所述完成實現,幾乎所有數組作用都可以通過命名索引非常順利地執行。當要求帶有命名索引的數組的長度長度時,就會出現問題。當引用“ array.prototype.length()”方法時,它僅返回帶有數字索引的數組的長度,如果我們使用命名索引,那就不好了,因為索引是字符串,但不再是數字。
In such a case if we need to return the length of the named indexed array then Object.keys(Arrayname).length would give the length of the array.The same is explained by taking three variables 'len1', 'len2', 'len3' where both 'len1', 'len3' store the lengths of a but 'len1' returns undefined and 'len3' returns 3 as the length of the array.
是JavaScript字符串對象的一部分的方法:
5.1字符串.charat():
var str1 = "akhil" ;
str1 . charAt ( - 1 ) ; // returns '' or empty string
str1 . charAt ( 3 ) ; // returns 'i' as it is located at position 3
str1 . charAt ( 7 ) ; // returns '' or empty string字符串.charat()是一種可用於確定給定字符串特定索引的字符的方法。該函數只需一個參數,它將字符作為輸出返回。應該注意的是,如果給出索引作為輸入,則如果大於或小於字符串長度,則該函數只是返回''或一個空字符串作為輸出。
5.2字符串.concat():
var str1 = "akhil" ;
var str2 = "pandey" ;
str1 . concat ( str2 ) ; // returns the string 'akhilpandey'
str1 . concat ( 1234 ) ; // returns the string 'akhil1234'
str1 . concat ( true ) ; // returns the string 'akhiltrue'
str1 . concat ( null ) ; // returns the string 'akhilnull'
str1 . concat ( undefined ) ; // returns the string 'akhilundefined'
str1 . concat ( [ 1 , 2 , 3 , 4 , 5 ] ) ; // returns the string 'akhil1,2,3,4,5'
str1 . concat ( " " , 12 , 34 , 56 , 78 ) ; // returns the string 'akhil 12345678'
str1 . concat ( { a : "123" , b : "456" } ) ; // returns the string 'akhil[object Object]'字符串.concat()是一種方法,用於將兩個或多個字符串組合起來,以便返回一個新字符串。從根本上講,此方法用於字符串操作,但是如果一個字符串與另一種類型串聯,則結果將是字符串。如果我們觀察到上面的示例,我們會看到str1.concat(true) ,因此在這裡,結果字符串為akhiltrue ,為字符串.concat()組合了參數的值,並產生一個串聯的字符串作為最終結果。現在,甚至有特殊情況,如果嘗試與虛假值string ,則結果將是string和虛假值的組合。
5.3字符串.indexof():
5.4字符串.lastIndexof():
5.5字符串.link():
5.6字符串.search():
5.4字符串.slice():
TIPS[Strings]:
string.slice() :方法string.slice()本質上是提取字符串的一部分,並返回切片的新字符串。使用String.slice方法的一般符號為String.Slice(pos1,pos2),其中pos1中的位置是起始索引的位置,而pos2是結束索引的位置。這裡要注意的要點是,如果我們通過傳遞負參數使用string.slice()方法,那麼它最終會從末端到開始對字符串進行計數並提取字符串。如果我們通過Donot傳遞第二個參數,則方法將提取字符串的其餘部分。
string.substring() :方法string.slice()和string.substring()屬於可用於從源字符串中提取字符串的部分或部分的相同類別的方法。兩者的區別在於,我們在使用方法字符串substring()時不能使用負索引。例如,如果我們將負面索引傳遞給諸如字符串的方法。 Substring(-7,-1),那麼它本質上不是輸出任何錯誤,表明使用負面索引犯了錯誤,而是顯示整個字符串
在JavaScript中:
| 類型 | 可以是對象 | 總是對象 |
|---|---|---|
| 布爾人 | ✔ | |
| 數字 | ✔ | |
| 字符串 | ✔ | |
| 日期 | ✔ | |
| 數學 | ✔ | |
| 正則表達式 | ✔ | |
| 數組 | ✔ | |
| 功能 | ✔ |
因此,基本上除了原始值除外,所有都是JavaScript中的對象
6.1 Objects can be created using three methods :
// creating an oject using an Object literal
var staff = {
name : "somename" ,
branch : "somebranch" ,
salary : "somesalary" ,
age : 20
} ; // creating an object using new keyword
var admin = new Object ( ) ;
admin . name = "somename" ;
admin . department = "somedept" ;
admin . userid = 123 ;
admin . age = 20 ; // creating an object using the object constructor
function student ( name , github_nick , url , age ) {
this . name = name ;
this . github = "https://github.com/" + github_nick ;
this . url = url ;
this . age = age ;
} 6.2 Using the constructor for the above defined Object :
var akhil = new student ( "Akhil Pandey" , "AkhilHector" , "https://www.akhilhector.com" , "20" ) ; 6.3 Accessing object methods :
console . log ( akhil . github ) ;
console . log ( akhil . url ) ; 6.4 Using the prototype property :
student . prototype . show = function ( ) {
return this . name + " " + this . github + " " + this . age ;
} 6.5 Adding methods to the prototype :
function student ( name , github_nick , url , age ) {
this . name = name ;
this . github = "https://github.com/" + github_nick ;
this . url = url ;
this . age = age ;
this . show = function ( ) {
return this . name + " " + this . github + " " + this . age ;
}
} NOTE :javasctipt對像是可變的,這意味著它們是由地址引用的,而不是價值。例如,如果“主”是一個對象,而“ master-backup”也是另一個對象,則如果我們通過對象,則更改一個對象可能會導致更改另一個對象。
{
var master = { foo : "foo" } ;
var master - backup = master ;
master . backup . bar = "bar" ; // this changes master.bar and master-backup.bar
}
Object . access . property = {
"Option1" : "we can use objectName.propertyName" ,
"Option2" : "Either we can write objectName[propertyName]"
} JavaScript中的功能關閉是關於如何在本地或全局範圍中處理和引用的變量。在JS變量中可以給出:
對於某種稱為私人變量的東西,沒有內置的概念,因此,當需要使用JS編寫此類方案時,以使范圍私有的變量範圍。
咖哩是通過獲取多個參數並通過一系列功能部分應用它們來構建功能的過程。因此,具體來說,您可以分解給定的功能,該功能將多個參數分為一系列參數的函數。
讓我們舉一個普遍的例子:
尾聲本質上無非是用循環替換遞歸功能的概念。在某種程度上,這不僅可以節省時間,還可以節省空間,即更好的時間複雜性和空間複雜性。
// snippet one
function f ( n ) {
if ( n == 0 ) {
return 1 ;
}
else {
return n * f ( n - 1 ) ;
}
}
// snippet two
function f1 ( n ) {
function r ( n , m ) {
if ( n == 0 ) {
return m ;
}
else {
return r ( n - 1 , n * m ) ;
}
}
return r ( n , 1 ) ;
}在觀察上面編寫的兩種算法中,我們可以理解F()是用於查找階乘的傳統遞歸方法,但是F1()是尾巴呼叫優化的算法,它更好,快速。
儘管已經採取了最好的努力來確保整個文檔無錯誤,但可能發生錯誤的情況可能會發生。在這種情況下,請提出問題,並幫助我改善此備忘單。