Skip to content

Commit 6e7bdcc

Browse files
author
liujun
committed
5.3
1 parent 5fada80 commit 6e7bdcc

4 files changed

Lines changed: 177 additions & 2 deletions

File tree

test/html/arrayTest.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464

6565
console.log(arr); // [4, 3, "chenyi"]
6666

67-
console.log(arr.splice(0,1,'i love my sun')); // [4] 返回的是删除元素
68-
console.log(arr); // ["i love my sun", 3, "chenyi"]
67+
console.log(arr.splice(0,1,'i love my chenyi')); // [4] 返回的是删除元素
68+
console.log(arr); // ["i love my chenyi", 3, "chenyi"]
6969

7070
console.log(arr.splice(1,2)); // [3,"chenyi"] 返回的是删除元素 当第二个参数为0时,返回[];因为没有删除任何元素
7171
console.log(arr); // ["i love my sun"]

test/html/loops.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
}
6363

6464

65+
6566

6667
</script>
6768
</body>

test/html/method.html

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
6+
<title>Examples</title>
7+
<meta name="description" content="">
8+
<meta name="keywords" content="">
9+
<link href="" rel="stylesheet">
10+
</head>
11+
<body>
12+
<script>
13+
// 在一个对象里绑定的函数,称为对象的方法
14+
var xiaoming = {
15+
alert: function(){
16+
console.log('alert重写');
17+
console.info(this);
18+
}
19+
}
20+
21+
22+
var oldAlert = window.alert;
23+
window.alert = xiaoming.alert;
24+
alert(); // alert重写 // window{}
25+
xiaoming.alert(); // alert重写 Object{}
26+
alert(3); // alert重写 // window{}
27+
alert = oldAlert;
28+
alert(3); // 3
29+
30+
// javascript 全局作用域只有一个
31+
32+
function getSex() {
33+
console.log(this.sex1);
34+
}
35+
36+
var chenyi = {
37+
name: 'chenyi',
38+
birth: 2014,
39+
sex1: 'boy',
40+
sex: getSex,
41+
age: function() {
42+
var y = new Date().getFullYear();
43+
console.info(y); // 2017
44+
return y - this.birth; // this => chenyi
45+
},
46+
height: function() {
47+
var _this = this;
48+
function getHeight() {
49+
return _this.birth + 90;
50+
}
51+
return getHeight();
52+
}
53+
}
54+
console.log(chenyi.age()); // 3
55+
// age(); // age is not defined
56+
57+
console.log(chenyi.height()); // NaN ==> 2104
58+
59+
chenyi.sex(); // boy
60+
getSex.apply(chenyi,[]); // boy
61+
62+
console.log(Math.max.apply(null,[7,1])); // 7
63+
console.log(Math.max.call(null,5,9)); // 9
64+
65+
// 调用普通函数时用null 绑定 this
66+
67+
68+
var oldParseInt = parseInt;
69+
var count = 0;
70+
window.parseInt = function() {
71+
count++;
72+
oldParseInt.apply(null,arguments);
73+
}
74+
parseInt(4);
75+
parseInt(4);
76+
parseInt(4);
77+
console.log(count); // 3
78+
</script>
79+
</body>
80+
</html>

test/html/variable.html

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
6+
<title>Examples</title>
7+
<meta name="description" content="">
8+
<meta name="keywords" content="">
9+
<link href="" rel="stylesheet">
10+
</head>
11+
<body>
12+
<script>
13+
14+
function a() {
15+
alert('声明的' + y);
16+
var y;
17+
y = 5;
18+
}
19+
a(); // undefined //变量声明会被提升 变量赋值不会被提升
20+
21+
22+
function b() {
23+
alert('没定义的' + y);
24+
25+
}
26+
// b(); // throw error: y is not defined
27+
28+
function abc() {
29+
var a = 1,
30+
b = 2,
31+
c = 3;
32+
console.info(a,b,c);
33+
}
34+
abc(); // 1,2,3
35+
36+
37+
var globalVariable = 'apple';
38+
console.log(window.globalVariable); // apple
39+
40+
console.log(count(9,7)); // 16 // 函数声明也会被提升
41+
function count(a,b) {
42+
return a + b;
43+
}
44+
45+
// console.info(count2(4,8)); // throw error : count2 is not a function
46+
var count2 = function (c,d) {
47+
return c + d;
48+
}
49+
50+
console.info(count2(4,8)); // 12 // 变量赋值不会被提升即使是函数
51+
52+
// 变量作用域在函数内部
53+
54+
function foo() {
55+
for(var i = 0; i < 4; i++) {
56+
57+
}
58+
console.log(i);
59+
}
60+
foo(); // 4
61+
62+
63+
function foo1() {
64+
for(let i = 0; i < 4; i++) {
65+
66+
}
67+
console.log(i);
68+
}
69+
foo1(); // undefined
70+
// let 声明块级作用域
71+
72+
73+
for(var i = 0; i < 6; i ++) {
74+
75+
setTimeout(function(){
76+
console.info(i); // 6
77+
})
78+
}
79+
80+
81+
82+
83+
for(let i = 0; i < 6; i ++) {
84+
85+
setTimeout(function(){
86+
console.info(i); // 0,1,2,3,4,5
87+
})
88+
}
89+
90+
91+
92+
</script>
93+
</body>
94+
</html>

0 commit comments

Comments
 (0)