这篇文章主要介绍了 JavaScript 知识点总结 (十一) 之 js 中的 Object 类详解的相关资料, 需要的朋友可以参考下
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
JavaScript 中的 Object 对象,是 JS 中所有对象的基类,也就是说 JS 中的所有对象都是由 Object 对象衍生的。Object 对象主要用于将任意数据封装成对象形式。
一、Object 类介绍
Object 类是所有 JavaScript 类的基类 (父类),提供了一种创建自定义对象的简单方式,不再需要程序员定义构造函数。
二、Object 类主要属性
1.constructor:对象的构造函数。
2.prototype:获得类的 prototype 对象,static 性质。
三、Object 类主要方法
1.hasOwnProperty(propertyName)
判断对象是否有某个特定的属性。必须用字符串指定该属性,例如,obj.hasOwnProperty("name"),返回布尔值。此方法无法检查该对象的原型链中是否具有该属性;该属性必须是对象本身的一个成员。
- var str = "";
- alert("str.hasOwnProperty(\"split\")的结果是:" + str.hasOwnProperty("split")); //return false
- alert("String.prototype.hasOwnProperty(\"split\")的结果是:" + String.prototype.hasOwnProperty("split")); //return true
运行结果:
hasOwnProperty 的用法不仅仅在此,在 Jquery 中在编写插件中,少不了的一步,就是初始化参数,其中一个很重要的方法就是 $.extend(); 他的原理就是应用了 hasOwnProperty() 方法;利用 for in 循环遍历对象成员中,有没有相同名称的对象成员,有的话就用这个新的对象成员替换掉旧的,通过这种方式,我们就可以通过修改方法中的参数变化,从而控制程序的流程,而对于那些没有改变的部分,仍使用默认值进行控制,我们自己也可以简单的模拟一下这个 extend 函数,如下
- function extend(target, source) { //target 旧的 source新的
- for (var i in source) {
- if (target.hasOwnProperty(i)) {
- target[i] = source[i];
- }
- }
- return target;
- }
- var a = {
- "first": ,
- "second": "lyl",
- "third": "bob"
- };
- var b = {
- "third": "leo"
- };
- extend(a, b);
- for (var i in a) {
- alert(a[i]); //原本是bob,现在变成leo了
- }
2.isPrototypeOf(object)
判断该对象是否为另一个对象的原型。
obj1.isPrototypeOf(obj2);
obj1 是 一个对象的实例;obj2 是另一个将要检查其原型链的对象。原型链可以用来在同一个对象类型的不同实例之间共享功能。如果 obj2 的原型链中包含 obj1,那么 isPrototypeOf 方法返回 true。如果 obj2 不是一个对象或者 obj1 没有出现在 obj2 中的原型链中,isPrototypeOf 方法将返回 false。
- <script type="text/javascript">
- function foo(){
- this.name = 'foo';
- }
- function bar(){
- }
- bar.prototype = new foo();
- var goo = new bar();
- alert(goo.name); //foo
- alert(bar.prototype.isPrototypeOf(goo));//true,在bar的原型链中有当前对象goo,则isPrototypeOf方法返回true
- </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:
- var p = new Object(); //通过Object直接创建对象
- //为p对象动态添加属性
- p.Age=;
- p.Name="孤傲苍狼";
- //扩展Object类,为Object类添加一个Show方法
- Object.prototype.Show=function(){
- alert(this.Age+"\t"+this.Name);
- }
- alert(p.Age);
- p.Show();
- document.write("<pre>");
- document.writeln("p.constructor:"+p.constructor);//得到对象的构造函数
- document.writeln("Object.prototype:"+Object.prototype);//得到prototype对象,prototype是静态属性,只能通过"类名.prototype"去访问
- document.writeln("p.isPrototypeOf(p):"+p.isPrototypeOf(p));
- document.writeln("p.hasOwnProperty(\"Age\"):"+p.hasOwnProperty("Age"));
- document.writeln("p.propertyIsEnumerable(\"Age\"):"+p.propertyIsEnumerable("Age"));
- document.writeln("p.toString():"+p.toString());
- document.writeln("p.valueOf():"+p.valueOf());
- document.write("</pre>");
运行结果:
测试代码 2:
- var Car = function(){};
- Car.prototype.hello = function(){
- alert("hello car");
- };
- var car = new Car();
- car.f = function() {
- alert("自定义方法");
- }
- document.write("<pre>");
- document.writeln("car.hasOwnProperty(\"f\")的结果是:"+car.hasOwnProperty("f"));//ture,car对象有f方法
- document.writeln("car.propertyIsEnumerable(\"f\")的结果是:"+car.propertyIsEnumerable("f"));//ture,car对象有f方法,f方法是可以被枚举的
- document.writeln("car.hasOwnProperty(\"hello\")"+car.hasOwnProperty("hello")); // false,因为car本身没有hello方法
- document.writeln("car.propertyIsEnumerable(\"hello\")的结果是:"+car.propertyIsEnumerable("hello")); // false,没有这个方法当然不能枚举
- document.writeln("car.constructor.prototype.hasOwnProperty(\"hello\")的结果是:"+car.constructor.prototype.hasOwnProperty("hello"));// true,car的类Car的原型有hello方法
- document.writeln("car.constructor.prototype.propertyIsEnumerable(\"hello\")的结果是:"+car.constructor.prototype.propertyIsEnumerable("hello"));// true, car的类的Car的原型hello方法是可以被枚举的
- document.writeln("Car.prototype.hasOwnProperty(\"hello\")的结果是:"+Car.prototype.hasOwnProperty("hello"));// true,car的类Car的原型有hello方法
- document.writeln("Car.prototype.propertyIsEnumerable(\"hello\")的结果是:"+Car.prototype.propertyIsEnumerable("hello"));
- document.write("</pre>");
运行结果:
来源: http://www.phperz.com/article/17/0331/265267.html