对象与属性和面向对象的特点

一、对象和属性中的一些方法

  1. instanceof
  2. prototype.isPrototypeOf
  3. hasOwnProperty
  4. in

1.instanceof 用于检测变量的指定数据类型

            function Student(name,age){
			    this.name=name
			    this.age=age
			}
			function Teacher(){
				
			}
			Student.prototype.show=function(){
			    conosole.log(this.name)
			}
			var s1=new Student("王一",21);
			console.log(s1 instanceof Student)
			console.log(s1 instanceof Teacher)

 可以用来检测创建出来的对象是通过哪个构造函数实例化出来的,也可以用来判断数据类型

2.prototype.isPrototypeOf判断对象是否是基于对应构造函数创建

和上面的instanceof基本一致但是他只能判断是否是对应的构造函数创建

            function Student(name,age){
			    this.name=name
			    this.age=age
			}
			function Teacher(){
				
			}
			Student.prototype.show=function(){
			    conosole.log(this.name)
			}
			var s1=new Student("王一",21);
			console.log(Student.prototype.isPrototypeOf(s1))
			console.log(Teacher.prototype.isPrototypeOf(s1))

 3.hasOwnProperty(属性名(key))用来检测对象中是否存在该属性

			function Student(name,age){
			    this.name=name
			    this.age=age
			}
			var s1=new Student("王一",21);
			console.log(s1.hasOwnProperty("name"))
			var s2={
				color:"white",
				address:"上海"
			}
			console.log(s2.hasOwnProperty("color"))

 但是使用hasOwnProperty无法检测到原型链中的方法和属性

			function Student(name,age){
			    this.name=name
			    this.age=age
			}
			Student.prototype.adc="123",
			Student.prototype.eat=function(){
				console.log("能吃")
			}
			var s1=new Student("王一",21);
			console.log(s1.hasOwnProperty("adc"))
			console.log(s1.hasOwnProperty("eat"))

 4.in 判断对象是否具有对应属性

该方法可以用来检测到创建在原型链中的方法

			function Student(name,age){
			    this.name=name
			    this.age=age
			}
			Student.prototype.adc="123",
			Student.prototype.eat=function(){
				console.log("能吃")
			}
			var s1=new Student("王一",21);
			console.log("eat" in s1)
			console.log("name" in s1)

 小案例:编写一个方法,传入一个对象 以及 key值,判断一下当前这个属性是否是存在于原型中的,如果是返回true,如果不是返回false

			function isprototype(obj,key){
				// 判断这个属性是否存在对象中
				if(key in obj){
					// 判断是否是本身所有的属性
					if(obj.hasOwnProperty(key)){
						return false
					}else{
						return true
					}
				}else{
					return false
				}
				// 精简写法
				// return key in obj&&obj.hasOwnProperty(key)==false
			}

二、面向对象的特点

  1. 抽象性:通过对象来分析实际问题
  2. 封装性:属性和方法 都封装到对象  节约开发成本  维护成本  安全
  3. 继承性 :类似java中的extends
  4. 多态性: 一个类 可以创建多种对象 ,js中基本用不

封装性:因为对象内部的属性,能直接通过对象.属性来进行修改,不安全,所以利用函数内部是局部变量的特点来新建,并且暴露出能提供修改和访问的方法setter和getter,在需要设置和查看的时候能直接调用函数

			function Person(name,age,phone){
				// phone无法直接查看和修改
				var phone=phone;
				this.name=name;
				this.age=age;
				// 设置查看的方法
				this.getter=function(){
					return phone
				}
				// 设置修改的方法
				this.setter=function(value){
					phone=value
				}
			}
			var p1=new Person("小明",18,11111111111);
			// console.log(p1.phone)//undefined
			console.log(p1.getter());//11111111111
			p1.setter(12312312312)
			console.log(p1.getter())//12312312312

继承性:

1.原型链继承

2.冒充继承

3.混合继承

1.原型链继承:将父类创建出来的对象赋值给子类的原型

2.冒充继承:通过call和apply来改变函数内部this的指向完成继承

call和apply的用法

        function Student(name,age){
            this.name=name
            this.age=age
            this.showInfo=function(){
                console.log(this.name)
            }
            this.abc=function(a,b,c){
                console.log(this.name+a+b+c)
            }
        }
        var s1=new Student("张三",21)
        var s2=new Student('李四',22)
        s1.showInfo()//张三
        s1.showInfo.call(s2)//把s1内部的this改成s2
        s1.showInfo()//李四
        //在使用call改变有参数的函数时需要用逗号分隔参数
        s1.abc.call(s2,2,3,4)
        //apply和call类似,不过call传参的方式是用数组
        s1.abc.apply(s2,[2,3,4])

apply:原本Math.max无法作用在数组里,通过apply改变后可以使用Math.max,由于没有需要替换的,所以填写null就可以使用Math.max方法

        var arr=[1,20,13,24,12]
        var max=Math.max.apply(null,arr)
        console.log(max)//24

3.混合继承:将原型链继承和冒充继承混合使用

猜你喜欢

转载自blog.csdn.net/cvenginner/article/details/121757197