码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript备忘复习笔记1

时间:2015-08-28 00:35:22      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

一.字符串操作

1.大小写

技术分享
var s = "hello";
undefined
g = s.toUpperCase();
"HELLO"
g;
"HELLO"
g.toLowerCase();
"hello"
View Code

2.索引/截断

技术分享
s.indexOf(‘o‘);
4
all.js:1 loading comments...
var l = "hello world";
undefined
l.substring(0,5);
"hello"
l.substring(7);
"orld"
View Code

 

二.数组

1.索引/长度

技术分享
var arr = [1, 2, 3.14, ‘Hello‘, null, true];
undefined
arr.length;
6
arr[2]=99;
99
arr;
[1, 2, 99, "Hello", null, true]
View Code

2.切片

技术分享
arr;
[1, 2, 99, "Hello", null, true]
arr.slice(1,3);
[2, 99]
View Code

3.pop/push与shift/unshift(从头删除,从头插入)

技术分享
arr;
[1, 2, 99, "Hello", null, true]
arr.pop();
true
arr;
[1, 2, 99, "Hello", null]
arr.push(1,"sd");
7
arr;
[1, 2, 99, "Hello", null, 1, "sd"]
arr.unshift(‘head‘,‘sec‘);
9
arr;
["head", "sec", 1, 2, 99, "Hello", null, 1, "sd"]
arr.shift();
"head"
arr;
["sec", 1, 2, 99, "Hello", null, 1, "sd"]
View Code

4.排序/翻转/指定索引删除

技术分享
var arr = [1, 2, 3.14, ‘Hello‘, null, true];
undefined
arr.sort();
[1, 2, 3.14, "Hello", null, true]
arr.reverse();
[true, null, "Hello", 3.14, 2, 1]
var arr = [‘Microsoft‘, ‘Apple‘, ‘Yahoo‘, ‘AOL‘, ‘Excite‘, ‘Oracle‘];
undefined
arr.splice(2, 3, ‘Google‘, ‘Facebook‘);
["Yahoo", "AOL", "Excite"]
arr;
["Microsoft", "Apple", "Google", "Facebook", "Oracle"]
arr.splice(2,2);
["Google", "Facebook"]
View Code

5.数组的拼接

技术分享
arr;
["Microsoft", "Apple", "Oracle"]
var lst = [‘A‘,‘B‘,‘C‘];
undefined
var lstadd = arr.concat(lst);
undefined
lstadd;
["Microsoft", "Apple", "Oracle", "A", "B", "C"]
lstadd.join(‘-‘);
"Microsoft-Apple-Oracle-A-B-C"
View Code

6.多维数组

 

三.对象

技术分享
var xiaoming = {
    name: ‘小明‘,
    birth: 1990,
    school: ‘No.1 Middle School‘,
    height: 1.70,
    weight: 65,
    score: null
};
undefined
xiaoming.name;
"小明"
xiaoming[‘name‘];
"小明"
‘name‘ in xiaoming;
true
xiaoming.hasOwnProperty(‘name‘);
true
View Code

 

四.循环

1.数组

技术分享
var arr = [‘Apple‘, ‘Google‘, ‘Microsoft‘];
for (var i = 0; i <= arr.length; i++) {
    alert(arr[i]);
}
View Code

2.列表

技术分享
var arr = ‘sdadda‘;
for (var i = 0; i <= arr.length; i++) {
    alert(arr[i]);
}
View Code

3.对象(类似python的dict)

技术分享
var o = {
    name: "jack",
    age: 20,
    city: ‘Beijing‘
};
for (var key in o){
    alert(o[key]);
}
View Code

 

五.Map/set(Map:类似python的dict)

1.Map取值

技术分享
var m = new Map([
    [‘Michael‘, 95], [‘Bob‘, 75], [‘Tracy‘, 85]
    ]);
alert(m.get(‘Michael‘));
View Code

2.Map增/删值

技术分享
var m = new Map();
m.set(‘alex‘,67);
m.set(‘bob‘,59);
alert(m.has(‘bob‘));
m.delete(‘bob‘);
alert(m.get(‘alex‘));
View Code

3.Set(去重,类似python列表的newlst = set(lst))

技术分享
var s2 = new Set([1, 2, 3]); 
undefined
s2;
Set {1, 2, 3}
s2.add(4);
Set {1, 2, 3, 4}
s2.delete(2);
true
s2;
Set {1, 3, 4}
all.js:1 loading comments...
s2.add(4);
Set {1, 3, 4}
View Code

 

六.迭代器(ArrayMapSet都属于iterable类型,用for..of迭代---PS:for..of是对for..in的修复)

1.for..of

技术分享
var a = [‘A‘,‘B‘,‘C‘];
var s = new Set([‘A‘, ‘B‘, ‘C‘]);
var m = new Map([[1,‘x‘],[‘name‘,‘y‘],[3,‘z‘]]);

for (var x of a){
    alert(x);
}
for (var x of s){
    alert(x);
}
for (var x of m){
    alert(x[0]+‘=‘+x[1]);
}
View Code

2.forEach

技术分享
//array
var a = [‘A‘, ‘B‘, ‘C‘];
a.forEach(function (element, index, array) {
    // element: 指向当前元素的值
    // index: 指向当前索引
    // array: 指向Array对象本身
    alert(element);
});

//Map
var m = new Map([[1, ‘x‘], [2, ‘y‘], [3, ‘z‘]]);
m.forEach(function (value,key,map){
    alert(value);
    alert(key);
});

//set无索引
var s = new Set([‘A‘, ‘B‘, ‘C‘]);
s.forEach(function (element, set) {
    alert(element);
});
View Code

 

Javascript备忘复习笔记1

标签:

原文地址:http://www.cnblogs.com/alexkn/p/4764864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!