1.------------------------>>驗證
//1和2區別: 預解釋問題,函數前后執行問題
//驗證名稱
function checkName(name) {
}
//驗證郵箱
function checkEmail(email) {
}
//驗證密碼
function checkPassword(password) {
}
2.------------------------>>
var checkName = function () {
return 'checkName';
};
var checkEmail = function () {
return 'checkEmail';
};
var checkPassword = function () {
return 'checkPassword';
};
3.------------------------>>對象封裝
var checkObj = {
checkName: function () {
},
checkEmail: function () {
},
checkPassword: function () {
}
};
checkObj.checkName();
/4.------------------------->>函數封裝(函數也是對象,函數三角色,對象,類,函數)
//對象不能復制一份,用new新創建的對象,不能繼承這些方法
// 將函數當作對象來使用的
var checkObj = function () {
};
checkObj.checkName = function () {
};
checkObj.checkEmail = function () {
};
checkObj.checkPassword = function () {
};
var s = new checkObj;
console.dir(s); //沒有這三個方法
5.-------------------------->> 克隆他們的方法,而不是直接調用,和checkObj沒有關系
var checkObj = function () {
return {
checkName: function () {
},
checkEmail: function () {
},
checkPassword: function () {
}
}
};
var a = checkObj();
a.checkName();
6------------------------->> JS類封裝 (類名第一個字母大寫,)
var CheckObj = function () { //公用方法每個對象克隆一份,浪費資源
this.checkName = function (name) {
},
this.checkEmail = function (email) {
},
this.checkPassword = function (password) {
}
};
var a = new CheckObj(); // new 一個類(函數)執行了四步操作: 1.函數執行 2.以當前類實例運行函數,3給這個實例對象綁定屬性和方法,this.xx =xx可以綁定上,4.返回這個實例對象
a.checkName();
7.-------------------------->>JS原型(prototype)
var CheckObj = function () {
};
CheckObj.prototype.checkName = function () {
};
CheckObj.prototype.checkEmail = function () {
};
CheckObj.prototype.checkPassword = function () {
};
//簡化
var CheckObj = function () {
};
CheckObj.prototype = {
constructor: CheckObj, //constructor指向類本身
checkName: function () {
return this;
},
checkEmail: function () {
return this;
},
checkPassword: function () {
return this;
}
};
var a = new CheckObj();
a.checkName().checkEmail().checkPassword();
//鏈式寫法 return this
var checkobj = {
checkName: function (name) {
return this;
},
checkEmail: function (email) {
return this;
},
checkPassword: function (password) {
return this; //最終返回這個對象
}
};
checkobj.checkName('clh').checkEmail(960526415).checkPassword(123456); //鏈式寫法 ,又返回這個對象
//函數祖先
Function.prototype.checkName = function (name) { //這樣內置內添加方法,會有覆蓋問題的
console.log(this === f); //--->>true 就是小f
if (name === 'clh')
console.log(name);
};
var f = function () {
};
f.checkName('clh'); //函數執行,前面有點,this就是點前面
//動態給Function類添加方法, 這樣是加的私有屬性
Function.prototype.addMethod = function (name, fn) {
this[name] = fn;
return this;
}
var methods = function () {
};
methods.addMethod('checkName', function () {
return this;
}).addMethod('checkEmail', function () {
return this;
}).addMethod('checkPassword', function () {
return this;
});
//在methods函數內增加三個私有的屬性
console.log(methods.hasOwnProperty('checkName')); //----->>true 是私有屬性
methods.checkName().checkEmail().checkPassword();
//鏈式添加方法
// 創建一個類,把你要添加的方法放到里面,
Function.prototype.myAddMenthod = function (name, fn) {
this.prototype[name] = fn;
return this;
}
var CheckObject = function () {
};
CheckObject.myAddMenthod('checkName', function () {
console.log('checkName');
return this;
}).myAddMenthod('checkEmail', function () {
return this;
}).myAddMenthod('checkPassword', function () {
return this;
});
var methods = new CheckObject();
methods.checkName(name).checkEmail().checkPassword()
Array.prototype.myAddMenthod = function (name, fn) {
this[name] = fn;
return this;
};
var ary = [1, 3, 4, 6, 0, 0, 0, 0, 1, 2, 3, 4, 4];
ary.myAddMenthod('unique', function () {
var obj = {};
for (var i = 0; i < this.length; i++) {
var curValue = this[i];
if (obj[curValue] === curValue) {
this[i] = this[this.length - 1];
this.length--;
i--;
continue;
}
obj[curValue] = curValue
}
return this;
}).myAddMenthod('getMaxValue', function () {
return Math.max.call(undefined, this);
}).myAddMenthod('getMinValue', function () {
return Math.min.call(undefined, this);
});
console.log(ary.unique().max().min());