zoukankan      html  css  js  c++  java
  • JavaScript学习总结(十一)——Object类详解

    一、Object类介绍

      Object类是所有JavaScript类的基类(父类),提供了一种创建自定义对象的简单方式,不再需要程序员定义构造函数。

    二、Object类主要属性

      1.constructor:对象的构造函数。

      2.prototype:获得类的prototype对象,static性质。

    三、Object类主要方法

      1.hasOwnProperty(propertyName)

      判断对象是否有某个特定的属性。必须用字符串指定该属性,例如,obj.hasOwnProperty("name"),返回布尔值。此方法无法检查该对象的原型链中是否具有该属性;该属性必须是对象本身的一个成员。

    1 var str ="";
    2 alert("str.hasOwnProperty("split")的结果是:"+str.hasOwnProperty("split")); //return false
    3 alert("String.prototype.hasOwnProperty("split")的结果是:"+String.prototype.hasOwnProperty("split"));//return true

    运行结果:

      

      hasOwnProperty的用法不仅仅在此,在Jquery中在编写插件中,少不了的一步,就是初始化参数,其中一个很重要的方法就是$.extend();他的原理就是应用了hasOwnProperty()方法;利用for in 循环遍历对象成员中,有没有相同名称的对象成员,有的话就用这个新的对象成员替换掉旧的,通过这种方式,我们就可以通过修改方法中的参数变化,从而控制程序的流程,而对于那些没有改变的部分,仍使用默认值进行控制,我们自己也可以简单的模拟一下这个extend函数,如下

     1 function extend(target,source){//target 旧的 source新的
     2   for (var i in source){
     3         if(target.hasOwnProperty(i)){
     4         target[i]=source[i];
     5         }
     6     }
     7     return target;
     8 }
     9 var a1={"first":1,"second":"lyl","third":"bob"};
    10 var b1={"third":"leo"};
    11 extend(a1,b1);
    12 for(var i in a1){
    13     alert(a1[i]);//原本是bob,现在变成leo了
    14 }

      2.isPrototypeOf(object)

      判断该对象是否为另一个对象的原型。

      obj1.isPrototypeOf(obj2);

      obj1是 一个对象的实例;obj2是另一个将要检查其原型链的对象。原型链可以用来在同一个对象类型的不同实例之间共享功能。如果obj2的原型链中包含 obj1,那么isPrototypeOf 方法返回 true。如果obj2不是一个对象或者obj1没有出现在obj2中的原型链中,isPrototypeOf 方法将返回 false。

     1  <script type="text/javascript">
     2     function foo(){
     3         this.name = 'foo';
     4     }
     5     function bar(){
     6 
     7     }
     8     bar.prototype = new foo();
     9     var goo = new bar();
    10     alert(goo.name); //foo
    11     alert(bar.prototype.isPrototypeOf(goo));//true,在bar的原型链中有当前对象goo,则isPrototypeOf方法返回true
    12   </script>

      3.propertyIsEnumerable(propertyName)

      通过这个方法我们可以检测出这个对象成员是否是可遍历的,如果是可遍历出来的,证明这个对象就是可以利用for in 循环进行遍历的,

      格式如下:obj.propertyIsEnumerable(propertyName)

      如果 propertyName存在于 obj中且可以使用一个 For…In 循环穷举出来,那么 propertyIsEnumerable 属性返回 true。如果 object 不具有所指定的属性或者所指定的属性不是可列举的,那么 propertyIsEnumerable 属性返回 false。典型地,预定义的属性不是可列举的,而用户定义的属性总是可列举的。

      4.toString():返回对象对应的字符串

      5.valueOf():返回对象对应的原始类型

      以上5个方法都是Object.prototype上定义的,ECMAScript 中的所有对象都由Object继承而来,所以在ECMAScript上的所有对象都具有以几个方法

    测试代码1:

     1 var p1 = new Object(); //通过Object直接创建对象
     2     //为p1对象动态添加属性
     3     p1.Age=20;
     4     p1.Name="孤傲苍狼";
     5     //扩展Object类,为Object类添加一个Show方法
     6     Object.prototype.Show=function(){
     7         alert(this.Age+"	"+this.Name);
     8     }
     9     alert(p1.Age);
    10     p1.Show();
    11     document.write("<pre>");
    12     document.writeln("p1.constructor:"+p1.constructor);//得到对象的构造函数
    13     document.writeln("Object.prototype:"+Object.prototype);//得到prototype对象,prototype是静态属性,只能通过"类名.prototype"去访问
    14     document.writeln("p1.isPrototypeOf(p1):"+p1.isPrototypeOf(p1));
    15     document.writeln("p1.hasOwnProperty("Age"):"+p1.hasOwnProperty("Age"));
    16     document.writeln("p1.propertyIsEnumerable("Age"):"+p1.propertyIsEnumerable("Age"));
    17     document.writeln("p1.toString():"+p1.toString());
    18     document.writeln("p1.valueOf():"+p1.valueOf());
    19     document.write("</pre>");

    运行结果:

      

    测试代码2:

     1 var Car = function(){};
     2     Car.prototype.hello = function(){
     3         alert("hello car");
     4     };
     5     var car = new Car();
     6     car.f = function() {
     7         alert("自定义方法");
     8     }
     9     document.write("<pre>");
    10     document.writeln("car.hasOwnProperty("f")的结果是:"+car.hasOwnProperty("f"));//ture,car对象有f方法
    11     document.writeln("car.propertyIsEnumerable("f")的结果是:"+car.propertyIsEnumerable("f"));//ture,car对象有f方法,f方法是可以被枚举的
    12     document.writeln("car.hasOwnProperty("hello")"+car.hasOwnProperty("hello")); // false,因为car本身没有hello方法
    13     document.writeln("car.propertyIsEnumerable("hello")的结果是:"+car.propertyIsEnumerable("hello"));   // false,没有这个方法当然不能枚举
    14     document.writeln("car.constructor.prototype.hasOwnProperty("hello")的结果是:"+car.constructor.prototype.hasOwnProperty("hello"));// true,car的类Car的原型有hello方法
    15     document.writeln("car.constructor.prototype.propertyIsEnumerable("hello")的结果是:"+car.constructor.prototype.propertyIsEnumerable("hello"));// true, car的类的Car的原型hello方法是可以被枚举的
    16     document.writeln("Car.prototype.hasOwnProperty("hello")的结果是:"+Car.prototype.hasOwnProperty("hello"));// true,car的类Car的原型有hello方法
    17     document.writeln("Car.prototype.propertyIsEnumerable("hello")的结果是:"+Car.prototype.propertyIsEnumerable("hello"));
    18     document.write("</pre>");

    运行结果:

      

  • 相关阅读:
    一个简单的knockout.js 和easyui的绑定
    knockoutjs + easyui.treegrid 可编辑的自定义绑定插件
    Knockout自定义绑定my97datepicker
    去除小数后多余的0
    Windows Azure Web Site (15) 取消Azure Web Site默认的IIS ARR
    Azure ARM (1) UI初探
    Azure Redis Cache (3) 创建和使用P级别的Redis Cache
    Windows Azure HandBook (7) 基于Azure Web App的企业官网改造
    Windows Azure Storage (23) 计算Azure VHD实际使用容量
    Windows Azure Virtual Network (11) 创建VNet-to-VNet的连接
  • 原文地址:https://www.cnblogs.com/MaxElephant/p/8118284.html
Copyright © 2011-2022 走看看