JS高级——Object.prototype成员

基本概念

成员描述
Object.prototype.__proto__ 指向当对象被实例化的时候,用作原型的对象。
Object.prototype.hasOwnProperty() 返回一个布尔值 ,表示某个对象是否含有指定的属性,而且此属性非原型链继承的。
Object.prototype.isPrototypeOf() 返回一个布尔值,表示指定的对象是否在本对象的原型链中。
Object.prototype.toString() 返回对象的字符串表示。
Object.prototype.valueOf() 返回指定对象的原始值。

valueOf

<script>

    function Person() {
        this.valueOf = function () {
            return 1;
        }
    }

    var p = new Person();

    //在对象参与运算的时候
    //1.默认的会先去调用对象的valueOf方法,
    //2.如果valueOf获取到的值,无法进行运算 ,就去去调用p的toString方法  最终做的就是字符串拼接的工作
    console.log(1 + p);
</script>

toString、toLocaleString

<script>
    // toString:转为字符串
    // toLocaleString:转为字符串,而且将对象转化成本地格式
    var o = {};
    console.log(o.toString());//[object Object]
    console.log(o.toLocaleString());//[object Object]

    var now = new Date();
    console.log(now.toString());//Mon Jan 22 2018 12:37:32 GMT+0800 (中国标准时间)
    console.log(now.toLocaleString());//2018/1/22 下午12:37:32
</script>

其他属性

<script>

    function Person() {
        this.name = 'qx';
    }
    //
    var p = new Person();
    //constructor:指向该原型对象相关联的构造函数
    console.log(p.constructor);
    //hasOwnProperty:用来判断对象本身(不包含原型)是否拥有某个属性
    console.log(p.hasOwnProperty("__proto__"));//false
    console.log(p.hasOwnProperty('name'));//true
    // propertyIsEnumerable:判断属性是否属于对象本身;判断属性是否可以被遍历
    console.log(p.propertyIsEnumerable('name'));//true
</script>
原文地址:https://www.cnblogs.com/wuqiuxue/p/8328413.html