【JavaScript】面向对象的程序设计
2024-09-30 02:36:49
一、前言
接着上一篇的内容,继续JavaScript的学习。
二、内容
属性类型
//数据属性
[Configurable] —— 能否通过delete删除属性从而重新定义属性,能否修改属性的特性,或者修改为访问器属性 默认为true
[Enumerable] —— 能否通过for-in循环返回属性 默认为true
[Writeable] —— 能否修改属性的值 默认为true
[Value] —— 包含这个属性的数据值 默认为undefined //要修改属性默认的特性,必须使用Object.defineProperty()
var person = {};
Object.defineProperty(person,"name",{
writable:false,
value:"Nicholas"
}); alert(person.name); //"Nicholas"
person.name = "Greg" //无效
创建对象
//hasOwnProperty()与in操作符
object.hasOwnProperty("propertyName") //在原型中返回false,在实例中返回true;
"propertyName" in object //无论存在于实例还是原型都返回true Object.keys(object.prototype); //取得对象上所有可枚举的实例属性
Object.getOwnPropertyNames(object.prototype) //取得对象上所有实例属性,无论是否枚举 //组合使用构造函数模式与原型模式
创建自定义类型的最常见方式,就是组合使用构造函数模式与原型模式。
定义实例属性 —— 构造函数模式
定义方法和共享属性 —— 原型模式
//传统方式
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.friends = ["Shelby","Court"];
}
Person.prototype = {
constructor:Person,
sayName: function(){
alert(this.name);
}
} //动态原型方式
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.friends = ["Shelby","Court"];
if(typeof this.sayName != "function"){
Person.prototype.sayName = function(){
alert(this.name);
};
}
}
继承
//确定原型和实例的关系
alert(instance instanceof Object); //true
alert(Object.prototype.isPrototypeOf(instance)); //true //伪造对象或经典继承
function SuperType(){
this.colors = ["red","blue","green"];
} function SubType(){
SuperType.call(this);
}
//组合继承 —— 需要两次调用超类型构造函数
function SuperType(name){
this.name = name;
this.color = ["red","blue","green"];
}
SuperType.prototype.sayName = function(){
alert(this.name);
} function SubType(name,age){
//继承属性
SuperType.call(this,name); //第二次
this.age = age;
}
//继承方法
SubType.prototype = new SuperType(); //第一次
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function(){
alert(this.age);
}
//原型式继承
var person = {
name:"Nicholas",
friends:["Shelby","Court","Van"]
}; var anotherPerson = Object.create(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
//或
var anotherPerson = Object.create(person,{
name:{
value:"Greg"
}
});
//寄生式继承
function createAnother(original){
var clone = object(original);
clone.sayHi = function(){
alert("Hi");
};
return clone;
}
var anotherPerson = createAnother(person);
anotherPerson.sayHi();
//寄生组合式继承 —— 一次调用超类型的构造函数
继承属性 —— 借用构造函数
继承方法 —— 原型链的混成形式 function inheritPrototype(subType,superType){
var prototype = object(superType.protoType);
prototype.constructor = subType;
subType.prototype = protype;
}
function SuperType(name){
this.name = name;
this.color = ["red","blue","green"];
}
SuperType.prototype.sayName = function(){
alert(this.name);
} function SubType(name,age){
//继承属性
SuperType.call(this,name);
this.age = age;
}
inheritPrototype(SubType,SuperType);
SubType.prototype.sayAge = function(){
alert(this.age);
}
最新文章
- SQL 语句与性能之联合查询和联合分类查询
- uva 147 Dollars
- Cocostudio 文章列表
- ubuntu下安装迅雷
- 【python】队列
- java 中 java.lang.ArrayIndexOutOfBoundsException: 0 异常
- 使用MySQL Proxy解决MySQL主从同步延迟
- python异常以及面向对象编程
- laravel Authentication and Security
- 猜测:信号槽的本质是使用Windows的自定义消息来实现的
- Node.js 使用gm处理图像
- 2017CCPC秦皇岛 M题Safest Buildings&;&;ZOJ3993【复杂模拟】
- ELK 5.6.8 安装部署
- indexes和indices的区别
- Windows文件共享,报错";该用户已禁用";解决方案
- 第9周 实现PWD命令
- Android四大组件之contentProvider
- 1.SpringMVC设计理念与DispatcherServlet
- HTML5的28个常用特性
- ORACLE启动 切换实例命令
热门文章
- JS基础,课堂作业,三个数字排序
- Android 7.1.1系统源码下载、编译、刷机-Nexus 6实战
- 简单在kubernetes中安装cadvisor
- Centos7部署Kubernetes集群(单工作节点)+配置dashboard可视化UI
- Java实现在线预览功能
- hdu - 6281,2018CCPC湖南全国邀请赛F题,快排
- IBM基于Kubernetes的容器云全解析
- ubuntu docker 安装
- 20172321 2018-2019《Java软件结构与数据结构》第三周学习总结
- c# Application.run和form.show区别