您好,登錄后才能下訂單哦!
轉(zhuǎn)載請(qǐng)注明出處
原文連接 http://blog.huanghanlian.com/article/5b698f22b8ea642ea9213f51
我們?cè)趺慈ツM重載,在javasceipr中我們可以通過(guò)參數(shù)的類型區(qū)別或者數(shù)量的區(qū)別,來(lái)去讓同樣一個(gè)函數(shù)名字,可以根據(jù)不同的參數(shù)列表的情況來(lái)去調(diào)用相應(yīng)的函數(shù)。
javascript中函數(shù)類型是不確定的,并且參數(shù)的個(gè)數(shù)也是可以任意的,那么我們可以通過(guò)判斷實(shí)際傳入的參數(shù)的個(gè)數(shù),來(lái)去做一個(gè)模擬的重載,
模擬重載
function person() {
var args = arguments;
if (typeof args[0] === 'object' && args[0]) {
if (args[0].name) {
this.name = args[0].name;
}
if (args[0].age) {
this.age = args[0].age;
}
} else {
if (args[0]) {
this.name = args[0];
}
if (args[1]) {
this.age = args[1];
}
}
};
person.prototype.toString = function() {
return "姓名:" + this.name + "年齡:" + this.age
}
var peng = new person({
name: "繼小鵬",
age: 23
});
console.log(peng.toString()); //姓名:繼小鵬年齡:23
var peng1 = new person("是你", 23);
console.log(peng1.toString()); //姓名:是你年齡:23
調(diào)用子類方法
例子1
if (!Object.create) {
Object.create = function(proto) {
function F() {};
F.prototype = proto;
return new F();
};
}
function person(name) {//基類
this.name=name;
}
person.prototype.init=function(){
console.log("你好"+this.name)
}
function student(name,classname){ //學(xué)生類
this.classname=classname;
person.call(this,name);
}
student.prototype = Object.create(person.prototype);
student.prototype.constructor = student;
student.prototype.init=function(){
console.log("你好s"+this.name)
}
var peng=new student("繼小鵬","class2");
console.log(peng);
peng.init();
例子2子類調(diào)用基類方法
function person(name) {//基類
this.name=name;
}
function student(name,classname){ //學(xué)生類
this.classname=classname;
person.call(this,name);
}
person.prototype.init=function(){
console.log(this.name)
}
student.prototype.init=function(){
person.prototype.init.apply(this,arguments);
}
var peng=new student("繼小鵬","class2");
console.log(peng);
peng.init();
鏈?zhǔn)秸{(diào)用
function classman() {}
classman.prototype.addClass = function(str) {
console.log('calss' + str + 'added');
return this;
}
var mang = new classman();
mang.addClass('classA').addClass('classB').addClass('classC')
// calssclassAadded
// calssclassBadded
// calssclassCadded
使用jq的時(shí)候$("#id").addClass('df')
選擇器做些操作后在繼續(xù)addClass('df')還可以再做動(dòng)作一層層鏈?zhǔn)饺フ{(diào)用。
例子解釋
function classman() {} //現(xiàn)定義一個(gè)構(gòu)造器classman
classman.prototype.addClass = function(str) { //給classman構(gòu)造器prototype添加addClass屬性方法
console.log('calss' + str + 'added'); //輸出表示添加一個(gè)class
return this; //return this表示返回classman的實(shí)例因?yàn)榉祷亓藢?shí)例那么緊接著后面不需要加mang.addClass('classA')直接后面加.addClass('classB').addClass('classB')就可以,每次執(zhí)行完都會(huì)返回實(shí)例
}
var mang = new classman();
mang.addClass('classA').addClass('classB').addClass('classC')
// calssclassAadded
// calssclassBadded
// calssclassCadded
抽象類
function Detectorlse() {
throw new Error("Abstract class can not be invoked directly!");
}
Detectorlse.detect = function() {
console.log('Detcetion starting...');
}
Detectorlse.stop = function() {
console.log('Detector stopped');
}
Detectorlse.init = function() {
throw new Error("Error");
}
function linkDetector() {};
linkDetector.prototype = Object.create(Detectorlse.prototype)
linkDetector.prototype.constructor = linkDetector;
//...add methods to LinkDetector...
defineProperty(ES5)
function Person(name) {
Object.defineProperty(this, 'name', {
value: name,
enumerable: true
});
};
Object.defineProperty(Person, 'arms_num', {
value: 2,
enumerable: true
});
Object.seal(Person.prototype);
Object.seal(Person);
function student(name, classname) {
this.classname = classname;
Person.call(this, name);
};
student.prototype = Object.create(Person.prototype);
student.prototype.constructor = student;
var peng = new Person('繼小鵬');
console.log(peng);
var han = new student("汗", "class2");
console.log(han);
模塊化
定義簡(jiǎn)單模塊化
var moduleA;
moduleA=function(){
var prop=1;
function func(){};
return {
func:func,
prop:prop
}
}();
定義簡(jiǎn)單模塊化2
var moduleA;
moduleA = new function() {
var prop = 1;
function func() {};
this.func = func;
this.prop = prop;
}();
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。