经过之前两篇的复习,现在我们主要看一些有代表的code,加深对之前的文档的理解。
//------
(function () {
var obj = {
show: function () {
this.display = "show";
},
hide: function () {
this.display = "hide";
}
}
alert(obj.display); //此时为undefined
obj.show(); //执行show函数后,将display属性跟obj对象联系起来了
alert(obj.display); //"show"
window.hide = obj.hide; //把window.hide指向obj.hide
window.hide(); //执行。hide的上下文对象是window对象了,所以this将指向window对象
alert(obj.display); //"show"。obj对象的display属性值不变,因为hide的上下文已经改变为window对象了
alert(window.display); //"hide"。 window对象的display属性被更新了
})();
(function () {
var obj = {
show: function () {
this.display = "show";
},
hide: function () {
this.display = "hide";
}
}
alert(obj.display); //此时为undefined
obj.show(); //执行show函数后,将display属性跟obj对象联系起来了
alert(obj.display); //"show"
window.hide = obj.hide; //把window.hide指向obj.hide
window.hide(); //执行。hide的上下文对象是window对象了,所以this将指向window对象
alert(obj.display); //"show"。obj对象的display属性值不变,因为hide的上下文已经改变为window对象了
alert(window.display); //"hide"。 window对象的display属性被更新了
})();
//---
//------------------
(function () {
window.name = "The Window";
var object = {
name: "My Object",
getNameFunc: function () {
return function () {
return this.name;
};
}
};
alert(object.getNameFunc()());
})();
//-------
(function () {
window.name = "The Window";
var object = {
name: "My Object",
getNameFunc: function () {
return function () {
return this.name;
};
}
};
alert(object.getNameFunc()());
})();
//-------
下面再看一个排序算法的CODE。
//-----------
(function () {
var i = 1;
console.log(i);
function Display(R1) {
var out = "";
for (var i = 0; i < R1.length; i++) {
out += " , " + R1[i];
}
console.log(out);
}
var r1 = [3, 2, 4, 7, 9, 1, 0, 5, 6];
function BubbleSort(R) {
var temp, flag = false;
for (var i = 0; i < R.length - 1; i++) {
flag = false;
for (var j = R.length - 1; j > i; j--) {
if (R[j] > R[j - 1]) {
temp = R[j];
R[j] = R[j - 1];
R[j - 1] = temp;
flag = true;
}
}
if (!flag) break;
}
Display(R);
}
function SelectSort(R) {
var min, k = 0, temp;
for (var i = 0; i < R.length - 1; i++) {
min = R[i];
for (var j = i + 1; j < R.length; j++) {
if (min > R[j]) {
min = R[j];
k = j;
}
}
if (R[i] != min) {
temp = R[k];
R[k] = R[i];
R[i] = temp;
}
}
Display(R);
}
BubbleSort(r1);
SelectSort(r1);
})();
//------------
(function () {
var i = 1;
console.log(i);
function Display(R1) {
var out = "";
for (var i = 0; i < R1.length; i++) {
out += " , " + R1[i];
}
console.log(out);
}
var r1 = [3, 2, 4, 7, 9, 1, 0, 5, 6];
function BubbleSort(R) {
var temp, flag = false;
for (var i = 0; i < R.length - 1; i++) {
flag = false;
for (var j = R.length - 1; j > i; j--) {
if (R[j] > R[j - 1]) {
temp = R[j];
R[j] = R[j - 1];
R[j - 1] = temp;
flag = true;
}
}
if (!flag) break;
}
Display(R);
}
function SelectSort(R) {
var min, k = 0, temp;
for (var i = 0; i < R.length - 1; i++) {
min = R[i];
for (var j = i + 1; j < R.length; j++) {
if (min > R[j]) {
min = R[j];
k = j;
}
}
if (R[i] != min) {
temp = R[k];
R[k] = R[i];
R[i] = temp;
}
}
Display(R);
}
BubbleSort(r1);
SelectSort(r1);
})();
//------------
待续.....