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()是尾巴呼叫优化的算法,它更好,快速。
尽管已经采取了最好的努力来确保整个文档无错误,但可能发生错误的情况可能会发生。在这种情况下,请提出问题,并帮助我改善此备忘单。