0
点赞
收藏
分享

微信扫一扫

对象的实例方法 Object.prototype

_karen 2022-03-30 阅读 208

 

Object.prototype.hasOwnProperty()

hasOwnProperty() 方法会返回一个布尔值,指示对象自身属性中是否具有指定的属性(也就是,是否有指定的键)。

参数:     要检测的属性的String,字符串形式表示的名称,或者Symbol

返回值: Boolean,存在true,不存在false.

const object1 = {};
object1.property1 = 42;

console.log(object1.hasOwnProperty('property1'));    // true

console.log(object1.hasOwnProperty('toString'));      // false  判断对象自身属性哦

console.log(object1.hasOwnProperty('hasOwnProperty'));    //  false 判断对象自身属性哦



//即使属性的值是 null 或 undefined,只要属性存在,依旧会返回 true。

object1.propOne = null;
console.log(object1.hasOwnProperty('propOne'));    // true

object1.propOne = undefined;
console.log(object1.hasOwnProperty('propOne'));    // true


// 遍历一个对象的所有自身属性
var buz = {
  fog: 'stack'
};

for (var name in buz) {
  if (buz.hasOwnProperty(name)) {
    console.log('this is fog (' +
      name + ') for sure. Value: ' + buz[name]);
  }
  else {
    console.log(name); // toString or something else
  }
}

Object.prototype.isPrototypeOf()

isPrototypeOf() 方法用于测试一个对象是否存在于另一个对象的原型链上。

参数:       在该对象的原型链上搜寻.

返回值:   Boolean, true表示在,false表示不在

function Foo() {}
function Bar() {}
function Baz() {}

Bar.prototype = Object.create(Foo.prototype);
Baz.prototype = Object.create(Bar.prototype);

var baz = new Baz();

console.log(Baz.prototype.isPrototypeOf(baz)); // true
console.log(Bar.prototype.isPrototypeOf(baz)); // true
console.log(Foo.prototype.isPrototypeOf(baz)); // true
console.log(Object.prototype.isPrototypeOf(baz)); // true

// 为了确保某些方法或属性将位于对象上。
// 例如,检查 baz 对象是否继承自 Foo.prototype:
if (Foo.prototype.isPrototypeOf(baz)) {
  // do something safe
}

Object.prototype.propertyIsEnumerable()

propertyIsEnumerable() 方法返回一个布尔值,表示指定的属性是否可枚举。

此方法可以确定对象中指定的属性是否可以被 for...in 循环枚举,但是通过原型链继承的属性除外。

参数:       需要测试的属性名

返回值:   Boolean, true表示是,false表示否

const object1 = {};
const array1 = [];
object1.property1 = 42;
array1[0] = 42;

console.log(object1.propertyIsEnumerable('property1'));  // true

console.log(array1.propertyIsEnumerable(0));             //  true

console.log(array1.propertyIsEnumerable('length'));      //  false



var a = [];
a.propertyIsEnumerable('constructor'); // 返回 false




Object.prototype.toLocaleString()

toLocaleString() 方法返回一个该对象的字符串表示。

返回值: 表示对象的字符串。

Object.prototype.toString()

toString() 方法返回一个表示该对象的字符串。

返回值: 一个表示该对象的字符串。

描述:  每个对象都有UI个toString()方法,当该对象被表示为一个文本值时,或者一个对象以预期的字符串方式引用时自动调用.

默认情况下,toString()方法被每个Object对象继承.

如果此方法在自定义对象中未被覆盖,toString()返回[object Object]

Object.prototype.valueOf()

valueOf() 方法返回指定对象的原始值。

 

示例: 使用valueOf

// Array:返回数组对象本身
var array = ["ABC", true, 12, -5];
console.log(array.valueOf() === array);   // true

// Date:当前时间距1970年1月1日午夜的毫秒数
var date = new Date(2013, 7, 18, 23, 11, 59, 230);
console.log(date.valueOf());   // 1376838719230

// Number:返回数字值
var num =  15.26540;
console.log(num.valueOf());   // 15.2654

// 布尔:返回布尔值true或false
var bool = true;
console.log(bool.valueOf() === bool);   // true

// new一个Boolean对象
var newBool = new Boolean(true);
// valueOf()返回的是true,两者的值相等
console.log(newBool.valueOf() == newBool);   // true
// 但是不全等,两者类型不相等,前者是boolean类型,后者是object类型
console.log(newBool.valueOf() === newBool);   // false

// Function:返回函数本身
function foo(){}
console.log( foo.valueOf() === foo );   // true
var foo2 =  new Function("x", "y", "return x + y;");
console.log( foo2.valueOf() );
/*
ƒ anonymous(x,y
) {
return x + y;
}
*/

// Object:返回对象本身
var obj = {name: "张三", age: 18};
console.log( obj.valueOf() === obj );   // true

// String:返回字符串值
var str = "http://www.xyz.com";
console.log( str.valueOf() === str );   // true

// new一个字符串对象
var str2 = new String("http://www.xyz.com");
// 两者的值相等,但不全等,因为类型不同,前者为string类型,后者为object类型
console.log( str2.valueOf() === str2 );   // false

改写 .prototype.valueof

举报

相关推荐

0 条评论