js面向对象
2016-06-25 10:37
399 查看
今天看了一些js面向对象的视频,js做的事真的好多
1、// 模拟重载
2、// 调用子类的方法
3、// 链式调用
4、// 抽象类
代码奉上
// 模拟重载
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() {
var str = 'name=' + this.name + ',age=' + this.age;
console.log(str)
return str;
}
var bosn = new Person('Bosn', 27);
bosn.toString();
var bosnO = new Person({
name: 'bosnO',
age: 38
});
bosnO.toString();
// 调用子类的方法
function Person(name) {
this.name = name;
}
function Student(name, className) {
this.className = className;
Person.call(this, name);
}
var bosn = new Student('Bosn', 'sw2');
Person.prototype.init = function() {
};
Student.prototype.init = function() {
// do sth
Person.prototype.init.apply(this, arguments);
}
// 链式调用
function classManager() {
//
}
classManager.prototype.addClass = function(str) {
console.log('Class:' + str + 'added');
return this;
};
classManager.addClass('classA').addClass('classB').addClass('classC');
// 抽象类
function DetectorBase() {
throw new Error('Abstract class can not be invoked directly!');
}
DetectorBase.detect = function() {
console.log('Detection starting...');
};
DetectorBase.stop = function() {
console.log('Detector stopping...');
};
DetectorBase.init = function() {
throw new Error('Error');
};
function LinkDetector() {
//
}
LinkDetector.prototype = Object.create(Detector.prototype);
LinkDetector.prototype.constructor = LinkDetector;
// add methods to LinkDetector...
1、// 模拟重载
2、// 调用子类的方法
3、// 链式调用
4、// 抽象类
代码奉上
// 模拟重载
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() {
var str = 'name=' + this.name + ',age=' + this.age;
console.log(str)
return str;
}
var bosn = new Person('Bosn', 27);
bosn.toString();
var bosnO = new Person({
name: 'bosnO',
age: 38
});
bosnO.toString();
// 调用子类的方法
function Person(name) {
this.name = name;
}
function Student(name, className) {
this.className = className;
Person.call(this, name);
}
var bosn = new Student('Bosn', 'sw2');
Person.prototype.init = function() {
};
Student.prototype.init = function() {
// do sth
Person.prototype.init.apply(this, arguments);
}
// 链式调用
function classManager() {
//
}
classManager.prototype.addClass = function(str) {
console.log('Class:' + str + 'added');
return this;
};
classManager.addClass('classA').addClass('classB').addClass('classC');
// 抽象类
function DetectorBase() {
throw new Error('Abstract class can not be invoked directly!');
}
DetectorBase.detect = function() {
console.log('Detection starting...');
};
DetectorBase.stop = function() {
console.log('Detector stopping...');
};
DetectorBase.init = function() {
throw new Error('Error');
};
function LinkDetector() {
//
}
LinkDetector.prototype = Object.create(Detector.prototype);
LinkDetector.prototype.constructor = LinkDetector;
// add methods to LinkDetector...
相关文章推荐
- JSP/Servlet 工作原理
- js数组的声明与应用
- Js内置对象的应用
- JavaScript对象
- js操作XML文件的实现方法兼容IE与FireFox
- 在页面中使用js
- Express.js中的locals用法
- Express.js中的locals用法
- js将滚动条滚动到指定位置的简单实现方法
- 在外部js文件中获取web工程名
- JSP简单运行原理----------九大隐式对象和四大域对象
- 编写一个jsp程序,实现用户登录,当用户输入的用户或密码错误时,将页面重定向到错误提示页,并在该页面显示30秒后 自动回到用户登录界面
- js删除局部变量的实现方法
- json解析
- javascript 广告移动特效的实现代码
- javascript简单特效,js学习
- NSJSONSerialization
- JSP用户登录页面
- js选项卡
- js节点属性