文章目录
前言
面向对象编程:从宏观出发,以对象作为业务研究依据,解决对应的问题
一、js四种创建方式
1.直接创建
var student={
name:'王一',
age:21,
eat:function(){
console.log(this.name+"正在吃")
}
}
//优点:创建简单
//缺点:无法复制
2.工厂模式
function Student(name,age){
var s=new Object()
s.name=name
s.age=age
s.eat=function(){
console.log(this.name+'正在吃')
}
return s
}
//优点:可以大批量复制调用;
//缺点:无法对明确对象的类型。都是object属性
3.构造函数
function Student(name,age){
this.name=name
this.age=age
this.eat=function(){
console.log(this.name+"正在吃")
}
}
//好处:可以明确对应的对象类型
//弊端:不同对象中的相同方法,无法公用,需要独立存储,造成内存损耗
4.原型创建
function Student(name,age){
this.name=name
this.age=age;
}
Student.prototype.eat=function(){
console.log(this.name+'正在吃')
}
二、各个元素的定义
1.this
function fun(){
console.log(this)//this指向window(运行环境)
}
fun()
2.new
通过new来创建对象 new做了什么?
1.创建一个空对象 {}
2.在执行函数的过程中 ,函数内部 this指向创建出来的{} this={} {name:“王一”,age:21,eat:function(){}}
3.将创建对象 返回出来,赋值给前方变量
var s1=new Student("王一",21)
console.log(s1)
typeof
返回对应变量的数据类型 返回根本数据类型 字符串
console.log(typeof(num)) //object number
instanceof
检测变量的数据类型 ,对应变量必须为原型创建出来的
console.log(num instanceof Object)//true\false
Prototype
属性 :原型对象(构造函数原型)
Student.prototype.eat=function(){
}
proto
实例化对象原型
console.log( s1.__proto__)
三、对象方法
变量的类型==判断是否在原型中
1.instanceof
function Student(name,age){
this.name=name
this.age=age
this.eat=function(){
console.log('吃饭')
}
} Student.prototype.showInfo=function(){
console.log(this.name+this.age)
}
function Teacher(name,age){
this.name=name
this.age=age
}
var s1=new Student("王一",21)
var t1=new Teacher("王二",22)
t1 instanceof Student//t1不在student原型中
2.isPrototypeOf()检测当前对象是否基于对应的构造函数创建出来的(天然的检测方法)
function Student(name,age){
this.name=name
this.age=age
this.eat=function(){
console.log('吃饭')
}
}
var s1=new Student("王一",21)
var result=Student.prototype.isPrototypeOf(t1);
console.log(result)//true
考试中会出现的检测方法,
if(Student.prototype.isPrototypeOf(t1)){
}
判断属性方法
1判断 属性或者方法 是 否属于 构造函数内部 .hasOwnProperty(属性)
var r=s1.hasOwnProperty("showInfo")
console.log(r)
2.判断当前对象是否具有对应属性 in
key in 对象
var r="color" in s1
consoloe.log(r)//true
//面试题:封装一个方法,传入一个对象,一个key,判断key是否在原型上,返回布尔值
function hasProtoType(obj,key){
return key in obj && obj.hasOwnProperty(key)==false
}
var r1=hasProtoType(s1,"aaa")
console.log(r1) //false
四、面向对象
面向对象编程特点:
1.抽象性: 通过对象来分析具体的问题
2.封装性: 将属性和方法都封装到对象中,方便统一管理 便于维护 便于二次开发 安全性较高(私有)
3.继承性: 将对象的属性和方法进行传递 ,Java C C++
4.多态性: 一个类 可以产生多种对象!js用不了!!
封装类
封装性:面向对象 可以将属性 分为两大类 一类公开属性 一类私有属性
公开属性:在任何位置都可以随便访问和修改 ,修改过程中没有任何的限制
私有属性: 不能随意访问,必须通过指定的方法来访问和修改
1.安全系数较高的属性
2.设置时不能随意设置
function User(name,age,password,phone){
var phone=phone
this.name=name
this.password=password
this.age=age
//get方法 来获取
this.getPhone=function(){
//判定
return phone
}
//set方法 来设置
this.setPhone=function(iphone){
var reg=/\d{11}/
if(reg.test(iphone)){
phone=iphone
}else{
alert("不符合规范")
}
}
}
var u1=new User("王一",21,"123456",15233645788)
console.log(u1.getPhone())//15233645788
u1.setPhone(18822127273)//如果位数不满11位,则无法改变
console.log(u1.getPhone())//18822127273
五、怎么设置私有属性
由于js没有私有属性的概念
利用闭包的思想,
将私有属性封装给局部变量;
var phon=phone;在函数运行之后就销毁。所以要从外部去拿
getphone//去获取电话
setphone//设置方法
call和apply
call和apply主要用于改变this的指向性
举例:
call
//将s1.showInfo() this指向s2对象
//call()方法 改变this的指向性 第一个参数 this更改为哪个对象
s1.showinfo.call(s2);//王二、把s1的对象改为s2
s1.abc.call(s2,1,2,3);//王二123、直接调用
apply
//apply有两个对象,一个是改变的对象,第二个是把参数整合到数组里
//apply(改变的对象,[1,2,3])
s1.abc.apply(s2,[1,2,3])//王二123