JavaScript学习笔记——对象知识点

javascript对象的遍历、内存分布和封装特性

一、javascript对象遍历

1.javascript属性访问
对象.属性
对象[属性] //字符串格式

//javascript属性的访问方法
   var ren ={};
   ren.name="张三";
   ren.sex="男";
   ren.eat=function  () {
      alert("吃饭");
   }
   alert(ren.name);
   alert(ren["name"]);

2.javascript属性遍历
for in

 //javascript属性遍历
 var ren ={};
   ren.name="张三";
   ren.sex="男";
   ren.eat=function  () {
      alert("吃饭");
   }

  for (var i in ren) {
     alert(ren[i])
  }

 通过arguments来遍历传入的参数

    function myArray () {
    var lengs=  arguments.length;
     for (var i=0; i<lengs; i++) {
        this[i]=arguments[i];
     }
    }
  var arr=new myArray(1,2,3);
  alert(arr[0]);


二、内存分布


三、对象的特性之封装
把对象所有的组成部分组合起来,尽可能的隐藏对象的部分细节,使其受到保护。
只保留有限的接口和外部发生联系。

  一、工厂函数

     //工厂函数
    function dianshi (color,size,brand) {
      var Tv={};
     Tv.color=color;
     Tv.size=size;
     Tv.brand=brand;
     Tv.look=function  () {
        alert("看电视");
     }
     Tv.play=function  () {
        alert("玩游戏");
     }
     Tv.dvd=function  () { 
        alert("DVD");
     } 
       return Tv;
      }

    var ds=dianshi("red","30inch","sony");
    //alert(typeof ds)
    alert(ds.color)

    var ds1=dianshi("blue","40inch","changh");
    alert(ds1["size"])

  二、构造函数 

    //构造方法的形式
    function Tv(color,size,brand) {
      this.color=color;
      this.size=size;
      this.brand=brand;
      this.play=function  () {
         alert("玩游戏");
      }
      this.look=function  () {
         alert("看电视");
      }
      this.dvd=function  () {
         alert("DVD");
      }
    }
    var sony=new Tv("red","20 inch","sony");
      alert(sony.color) 

  三、prototype方法

  对原型属性的修改将影响到所有的实例

 //prototype方法
    function Tv(color,size,brand) {
      this.color=color;
      this.size=size;
      this.brand=brand;
      this.play=function  () {
         alert("玩游戏");
      }
    
    }

      Tv.prototype.look=function  () {
         alert("看电视");
      }
      Tv.prototype.dvd=function  () {
         alert("DVD");
      }
      Tv.prototype.aaa={name:"张三"};
    var sony=new Tv("red","20 inch","sony");
    var changhong =new Tv("red","20 inch","CH");
//      delete sony.color
//      delete sony.play
//      delete sony.look
//      alert(sony.color)
//       alert(sony.play)
//       alert(sony.look)
//   sony.look();
//   changhong.look();

alert(sony.aaa.name="李四");
alert(changhong.aaa.name);

  四、混合方法

//混合方式
function Tv(color,size,brand) {
      this.color=color;
      this.size=size;
      this.brand=brand;
      this.play=function  () {
         alert("玩游戏");
      }
    
     Tv.prototype.aaa={name:"张三"};

    }

      Tv.prototype.look=function  () {
         alert("看电视");
      }
      Tv.prototype.dvd=function  () {
         alert("DVD");
      }

javascript对象的继承和Object对象

对象的一个类可以从现有的类中派生,并且拥有现有的类的方法或是属性,这和过程叫做继承。被继承的类叫做父类或是基类,继承的类叫做子类。

(一个对象拥有另一个对象的属性和方法)


优点:

提高代码的重用性

提高代码的可维护性

提高代码的逻辑性


一、Object对象

var obj=new Object()

属性:

  1.constructor
  对创建对象的函数的引用(指针)。

   //1.constructor 
       //对创建对象的函数的引用(指针)
   
     var obj=new Object();
    alert(obj.constructor)


  2.Prototype 原型

  **********************************************
  对该函数对象的对象原型的引用。是函数对象的默认属性

  **********************************************

//Prototype  
      //对该函数对象的对象原型的引用。

     var obj=new fun1();
     function fun1 () {
      this.name="zhangsan";
    }
     alert(obj.prototype)
    alert(fun1.prototype)

  A.对象的共享属性存放到代码段当中。

  B.可以实现继承。

方法:

  A.hasOwnProperty(property)
  判断对象是否有某个特定的属性,返回true或者false

alert(obj.hasOwnProperty("name"))

  B.IsPrototypeOf(object)
  判断该对象是否为另一个对象的原型。(用来检测对象的类型)

  var arr=new Array();
  alert(Array.prototype.isPrototypeOf(arr))

  c.运算符
  instanceof

   java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例

alert(arr instanceof Array)


二、继承

1.原型继承

function person () {
  this.name="张三";
  this.say=function  () {
    alert(this.name)
  }
}

function student () {
}
student.prototype=new person()

var zhangsan=new student ();
zhangsan.say()

2.对象冒充的形式
  A.call
  obj1.fun.call(obj2,参数1......)

  B.apply
  obj1.fun.call(obj2,[参数1,参数2....])

  让对象1的方法冒充成对象2的方法。

//对象冒充
/*
function person () {
   this.name="张三";
   this.say=function  () {
     alert(this.name)
   }
}

function student () {
  this.name="李四";
}
var ren=new person ();
var zhangsan=new student ();

ren.say.call(zhangsan)

*/
function person (name) {
   this.name=name;
   this.say=function  () {
     alert(this.name)
   }
}

function student () {
 window.person.apply(this,["zhangsan"])
}

var zhangsan=new student ();
  alert(zhangsan.name)
  zhangsan.say();

  

对象的继承顺序

一、对象的继承顺序

//对象的继承顺序
    Object.prototype.say=function  () {
       alert("我是顶层的方法");
    }
    function person () {
        this.say=function  () {
           alert("我是父类的方法");
        }
    }

    person.prototype.say=function  () {
         alert("我是父类原型的方法");
    }

    function study () {
    this.say=function  () {
       alert("本身的方法");
    }
       
    }
    study.prototype=new person();
     study.prototype.say=function  () {
       alert("本身原型的方法");
     } 
    var zhangsan=new study ();
    alert(zhangsan.say)


原文地址:https://www.cnblogs.com/tonglin0325/p/4712985.html