OOP面向对象编程(下)
我们怎么去模拟重载,在javasceipr中我们可以通过参数的类型区别或者数量的区别,来去让同样一个函数名字,可以根据不同的参数列表的情况来去调用相应的函数。
javascript中函数类型是不确定的,并且参数的个数也是可以任意的,那么我们可以通过判断实际传入的参数的个数,来去做一个模拟的重载,
###OOP(模拟重载、链式调用、模块化)
模拟重载
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
调用子类方法
例子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){ //学生类
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子类调用基类方法
function person(name) {//基类
this.name=name;
}
function student(name,classname){ //学生类
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();
链式调用
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的时候$(“#id”).addClass(‘df’) 选择器做些操作后在继续addClass(‘df’)还可以再做动作一层层链式去调用。
例子解释
function classman() {} //现定义一个构造器classman
classman.prototype.addClass = function(str) { //给classman构造器prototype添加addClass属性方法
console.log('calss' + str + 'added'); //输出表示添加一个class
return this; //return this表示返回classman的实例因为返回了实例那么紧接着后面不需要加mang.addClass('classA')直接后面加.addClass('classB').addClass('classB')就可以,每次执行完都会返回实例
}
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);
模块化
定义简单模块化
var moduleA;
moduleA=function(){
var prop=1;
function func(){};
return {
func:func,
prop:prop
}
}();
定义简单模块化2
var moduleA;
moduleA = new function() {
var prop = 1;
function func() {};
this.func = func;
this.prop = prop;
}();
http://www.huanghanlian.com/javascript/2016/12/20/javascript_jinjie9.html
http://www.huanghanlian.com/javascript/2016/12/20/javascript_jinjie8.html(上卷)
OOP面向对象编程(下)的更多相关文章
- Java实现OOP(面向对象编程)
一.对象的综述 面向对象编程(OOP)具有多方面的吸引力.对管理人员,它实现了更快和更廉价的开发与维护过程.对分析与设计人员,建模处理变得更加简单,能生成清晰.易于维护的设计方案.对程序员,对象模型显 ...
- 用C实现OOP面向对象编程(1)
如摘要所说,C语言不支持OOP(面向对象的编程).并这不意味着我们就不能对C进行面向对象的开发,只是过程要复杂许多.原来以C++的许多工作,在C语言中需我们手动去完成. 博主将与大家一起研究一下如下用 ...
- Go语言基础之接口(面向对象编程下)
1 接口 1.1 接口介绍 接口(interface)是Go语言中核心部分,Go语言提供面向接口编程,那么接口是什么? 现实生活中,有许多接口的例子,比如说电子设备上的充电接口,这个充电接口能干什么, ...
- 一百零六、SAP的OOP面向对象编程,OO-ALV的简介
面向对象编程,如图 基本概念: 1.对象(Object)是一个现实实体的抽象.一个对象可被认为是一个把数据(属性)和程序(方法)封装在一起的实体,这个程序产生该对象的动作或对它接受到的外界信号的反应. ...
- Swift -POP( 面向协议编程)与OOP(面向对象编程)
面向协议编程(Protocol Oriented Programming,简称POP),是Swift的一种编程范式,Apple于2015年WWDC提出的,如果大家看Swift的标准库,就会看到大量PO ...
- (转)OOP(面向对象编程)的几大原则
文章转载自:http://blog.csdn.net/anders_zhuo/article/details/8949566 设计模式遵循的一般原则: 1.开-闭原则(Open-Closed Prin ...
- python面向对象编程(下)
本篇详细介绍了Python 中类的成员.成员修饰符.类的特殊成员以及两个综合运用实例. 环境为:python3.5.1 类的成员 类的成员包括三大类:字段.方法和property属性 注:关于这三类成 ...
- OOP(面向对象编程)的一些特性
接口:接口是把公共实例(非静态)方法和属性结合起来,以封装特定功能的一个集合.一旦定义了接口,就可以在类中实现它.接口注意事项:接口不能单独存在.不能像实例化一个类那样实例化接口.另外,接口不能包含实 ...
- 重学C++ (十一) OOP面向对象编程(2)
转换与继承 本节主要须要区分的是: 基类和派生类的转换: 引用(指针)的转换和对象的转换. 1.每一个派生类对象包括一个基类部分.因此.能够像使用基类对象一样在派生类对象上执行操作. 基于这一点,能够 ...
随机推荐
- windows下局域网文件共享,不需要登录账号密码
基于局域网中,有时候需要传输一个大的文件都需要用到U盘,很麻烦,所以优选文件共享.但是有时候会出现需要登录账户密码,所以需要设置第三步,步骤如下: 1.选中要共享的文件夹,右键有一个共享选项,点击出现 ...
- python 文档
python 文档 https://docs.python.org/2/library/index.html
- Html.Partial,Html.RenderPartial Html.Action,Html.RenderAction区别
@Html.Partial,@Html.RenderPartial 这两者的共同点都是在视图中去调用另外一个视图,区别是 Html.Partial 有返回值 ( MvcHtmlStrin ...
- maven报 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.0:compile(defalut-compile) on project 项目名称:No such compile 'javac'
这个问题纠结了一天,在另外一个电脑是正常的,但是从服务器下载下来到另外一个电脑的时候却出现了如下图问题 看到javac大家都会想到是编译出现问题,而本地的配置如下图所示: 看着配置都是一致的,会是哪里 ...
- 为何GET只发一次TCP连接,POST发两次TCP连接
GET和POST是HTTP请求的两种基本方法,要说他们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...
- 转载:【Scala】使用Option、Some、None,避免使用null
转载自Jason DingGitCafe博客主页(http://jasonding1354.gitcafe.io/)Github博客主页(http://jasonding1354.github.io/ ...
- linux/nginx命令
1.ps查看服务器所有的进程: -aux 显示所有状态 -ef 简洁信息 ps -aux | grep node 第二列是pid,杀掉程序使用kill. ps -ef | grep node 第一列是 ...
- 一次完整的从webshell到域控的探索之路
前言 内网渗透测试资料基本上都是很多大牛的文章告诉我们思路如何,但是对于我等小菜一直是云里雾里. 于是使用什么样的工具才内网才能畅通无阻,成了大家一直以来的渴求. 今天小菜我本着所有师傅们无私分享的精 ...
- 两矩阵各向量余弦相似度计算操作向量化.md
余弦相似度计算: \cos(\bf{v_1}, \bf{v_2}) = \frac{\left( v_1 \times v_2 \right)}{||v_1|| * ||v_2|| } \cos(\b ...
- C#在WinForm下使用HttpWebRequest上传文件
转自:http://blog.csdn.net/shihuan10430049/article/details/3734398 这段时间因项目需要,要实现WinForm下的文件上传,个人觉得采用FTP ...