首页 > 网站 > WEB开发 > 正文

JavaScript Object对象

2024-04-27 14:10:48
字体:
来源:转载
供稿:网友

javaScript Object对象

Object对象

1. 介绍

  Object对象,是所有Javascript对象的超类(基类)。Object.PRototype(Obecjt的原型)定义了Js对象的基本方法和属性。

2. 构造函数

2.1 new Object() :返回一个Object实例

2.2 new Object(value) :根据value的值,返回不同的对象(Number、Boolean、String)

参数:

①value {number | bool | string} :一个数字、布尔值或者字符串

返回值:

{Number、Boolean、String} 返回一个转换后的对象

示例 :

var o = new Object(123);console.log(o); // => Number对象o = new Object(true);console.log(o); // => Boolean对象o = new Object('abc');console.log(o); // => String对象

3. 实例属性

3.1__proto__ :设置或返回对象的原型对象(IE中不支持此属性)

说明:

1) 赋值时,对象继承新原型的所有方法和属性,以及新原型的原型链中的所有方法和属性。

2) 属性名称以两个下划线开始和结束。

3) 对象的__proto__ == 对象类的prototype

示例:

// 1.自定义对象多层继承function People(name) {    this.name = name;}function Student(age) {    this.age = age;}Student.prototype = new People(); // 设置Student的原型为People对象var s = new Student(22);console.log(s.__proto__); // => People 对象console.log(Student.prototype); // => People 对象console.log(s.__proto__ == Student.prototype); // => true// 2.对象直接量 var p = {}; // 等于new Object()console.log(p.__proto__ == Object.prototype); // => true

3.2prototype :设置或返回对象类的原型对象

说明:

1) prototype为对象类的属性。__proto__是对象的属性。

2) Js内置对象(Array、Date等对象)都有一个只读的prototype属性。 可将属性和方法添加到原型中,但不能为内置对象分配其他原型。

3) 自定义对象的prototype属性可进行读写操作。

示例:

var Student = function (name) {    this.name = name;};// 给Student的原型添加一个sayHello方法Student.prototype.sayHello = function () {    alert('Hello,' + this.name);}var st = new Student('张三'); // 初始化对象stconsole.log(st.name); // => 张三st.sayHello(); // 弹出:Hello,张三

3.3 constructor:表示创建此对象的构造函数

说明:

1) 设置或返回创建此对象的构造函数。

2) 若一个对象有多层继承,将返回最先调用的构造函数。

3) obj.constructor.prototype 可表示对象的原型。

示例:

// 1.内置对象var str = 'abc';console.log(str.constructor); // => function String 构造函数var o = {};console.log(o.constructor); // => function Object 构造函数// 2.自定义对象多层继承 :constructor返回最先调用的构造函数function People(name) {    this.name = name; // s对象初始化时,先调用People构造函数,再调用Student构造函数    console.log('People调用');}function Student(age) {    this.age = age;    console.log('Student调用');}Student.prototype = new People(); // 设置Student的原型为People对象var s = new Student(22);console.log(s.constructor); // => function People 构造函数

总结:__proto__、prototype、constructor 的关系

说明:

1) 对象的__proto__ 等于 类的prototype

2) 对象的constructor 等于 类,所以obj.constructor.prototype 可表示对象的原型。

示例:

var o = {};console.log(o.__proto__ === Object.prototype); // true :对象的__proto__等于类的prototypeconsole.log(o.constructor === Object); // true :对象的constructor 等于 类console.log(o.constructor.prototype === Object.prototype); // true :o.constructor.prototype 可表示对象的原型。

  

4. 实例方法

4.1hasOwnProperty(propertyName) :判断对象是否拥有一个指定名称的实例属性(非继承)

参数:

①propertyName {string} :属性名称。

返回值:

{bool} 判断对象是否拥有一个指定名称的本地定义(非继承)的属性;此方法不会检查对象原型链中的属性。

true :属性为对象的实例属性,非继承。

false :属性不为对象的实例属性。

示例 :

// 1.Object对象var o = new Object();o.name = '自定义属性'; // 定义一个实例属性console.log(o.hasOwnProperty('name')); // => true:name属性为实例o自己定义的,而非继承console.log(o.hasOwnProperty('toString')); // => false:toString为继承属性// 2.自定义对象var Student = function (name) {    this.name = name;};// 给Student的原型添加一个sayHello方法Student.prototype.sayHello = function () {    alert('Hello,' + this.name);}// 给Student的原型添加一个age属性Student.prototype.age = '';var st = new Student('张三'); // 初始化对象stconsole.log(st.hasOwnProperty('name')); // => true :调用构造函数时,通过this.name附加到实例对象上console.log(st.hasOwnProperty('sayHello')); // => false :sayHello方法为原型上的成员console.log(st.hasOwnProperty('age')); // => false :age属性为原型上的成员

4.2isPrototypeOf(obejct) :判断某个原型是否出现在对象的原型链中

语法:

prototype.isPrototypeOf(object)

参数:

①obejct{object} :被检测的对象。

返回值:

{bool}返回某个原型是否出现在对象的原型链中

true :是

false :不是

示例 :

// 1.Obejct对象var o = new Object();console.log(Object.prototype.isPrototypeOf(o)); // => true :o为Obejct一个对象// 2.Arrayvar array = [1, 2, 3];console.log(Array.prototype.isPrototypeOf(array)); // => true :数组原型console.log(Object.prototype.isPrototypeOf(array)); // => true :Object是所有对象的基原型// 3.自定义对象var People = function () {}var Student = function () {}// 设置Student类的原型为PeopleStudent.prototype = new People();var st = new Student();console.log(Student.prototype.isPrototypeOf(st)); // => true :st为Student一个对象console.log(People.prototype.isPrototypeOf(st)); // => true :Student的原型为Peopleconsole.log(Object.prototype.isPrototypeOf(st)); // =>true :Object是所有对象的基原型

4.3propertyIsEnumerable(propertyName) :判断指定名称的属性是否为实例属性并且是可枚举的(可用for/in循环枚举)

参数:

①propertyName{string} :属性名称

返回值:

{bool} 判断属性是否为实例属性并且是可枚举的(可用for/in循环枚举),不考虑原型链中的成员。

true :是

false :不是

示例 :

// 1.Array对象var array = [1, 2, 3];array.name = 'Array';console.log(array.propertyIsEnumerable('name')); // => true :name属性为实例属性console.log(array.propertyIsEnumerable('join')); // => false :join方法继承自Arrayconsole.log(array.propertyIsEnumerable('length')); // => false :length属性继承自Arrayconsole.log(array.propertyIsEnumerable('toString')); // => false :toString方法继承自Object// 2.自定义对象var Student = function (name) {    this.name = name;}// 定义一个原型方法Student.prototype.sayHello = function () {    alert('Hello' + this.name);};var a = new Student('tom');console.log(a.propertyIsEnumerable('name')); // => true :name为自身定义的实例属性console.log(a.propertyIsEnumerable('age')); // => false :age属性不存在,也返回falseconsole.log(a.propertyIsEnumerable('sayHello')); // => false :sayHello属于原型方法

4.4toLocaleString() :返回当前对象的一个本地化的字符串表示

4.5toString() :返回当前对象的一个字符串表示形式

4.6valueOf() :返回当前对象的原始值

参数:

返回值:

{object}返回当前对象关联的原始值,若没有相关联的值,则返回对象本身

示例 :

var a = [1, 2, 3];console.log(a.valueOf()); // => [1, 2, 3]var b = true;console.log(b.valueOf()); // => truevar c = {};console.log(c.valueOf()); // => Object {}var s = 'abc';console.log(s.valueOf()); // => abc// 自定义个对象,重写valueOfvar customObject = {};customObject.valueOf = function () {    return '自定义对象';}console.log(customObject.valueOf()); // => 自定义对象

5. 静态方法

5.1Object.create(prototype, propertyDescriptor):创建并返回一个指定原型和指定属性的对象

参数:

①prototype {prototype} :返回对象的原型,可以为null。若为null,对象的原型为undefined。

②propertyDescriptor {propertyDescriptor} 可选:属性描述符。

属性描述符:设置属性的一系列特性;

语法格式:

propertyName: {    value: '', // 设置此属性的值    writable: true, // 设置此属性是否可写入;默认为false:只读    enumerable: true, // 设置此属性是否可枚举(通过for/in预付);默认为false:不可枚举    configurable: true // 设置此属性是否可配置;如:是否可以修改属性的特性及删除属性。默认为false}

返回值:

{object}返回一个指定原型和指定属性的对象

示例 :

// 建立个自定义对象,设置name和age属性var obj = Object.create(null, {    name: {        value: 'tom',        writable: true,        enumerable: true,        configurable: true    },    age: {        value: 22    }});console.log(obj.name); // => tomconsole.log(obj.age); // => 22obj.age = 28;console.log(obj.age); // => 22 :age属性的writable默认为fal
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表