zoukankan      html  css  js  c++  java
  • Object.prototype 与 Function.prototype 与 instanceof 运算符

    方法:

    1. hasOwnProperty
    2. isPrototypeOf
    3. propertyIsEnumerable

    hasOwnProperty

    该方法用来判断一个对象中的某一个属性是否是自己提供的( 住要用在判断属性是原型继承的还是自己提供的 )

    语法: 对象.hasOwnProperty( '属性名' ) -> boolean

    isPrototypeOf

    凡是看到 of 翻译成 的, 反过来翻译: prototype of obj, 翻译成 obj 的 原型

    因此该方法的含义是: xxx 是 xxxx 的原型

    语法: 对象.isPrototypeOf( 对象 ) -> boolean

    propertyIsEnumerable(了解)

    这个方法用于判断对象的某一个属性是不是自己提供的( 与 hasOwnProperty 一样 ), 同时该属性要求可枚举( for-in 遍历出来)

    其他

    1. toString
    2. toLocaleString
    3. valueOf

    例: var d=new Date();

      console.log(d.toString);

      console.log(d.toLocalString);

    Function.prototype

    常用成员

    1. apply 和 call
    2. caller
    3. bind

    apply 和 call(在函数的四种调用模式中已详细说过)

    caller

    了解, 一般不推荐使用. 获得函数的调用者.

    概念: 调用者与被调用者

        function foo1() {
            foo2();
        }
    

    bind

    绑定, 这个语法来源于 ES5

    在获得页面元素的时候, 是否有想过将 document.getElementById 用函数存起来

        var f = document.getElementById;
        // f( 'id' );
        f.call( document, 'id' );

    instanceof 运算符

    愿意: 判断某一个对象是否为某一个函数的实例. 就是判断对象是不是由指定构造方法所创建.

        function Person() {}
        var p = new Person();
    
        console.log( p instanceof Person );
        // p -> Person.prototype -> Object.prototype -> null
        console.log( p instanceof Array );
    

    案例:

        console.log( Object instanceof Function );
        console.log( Function instanceof Object );
    

    instanceof 表示判断构造函数的原型属性是否在对象所在的原型链上.

    语法: 对象 instanceof 函数 -> boolean

    描述: 说 '函数.prototype' 是否在 '对象' 的原型链上

     
  • 相关阅读:
    [CF1439B] Graph Subset Problem
    [CF1439C] Greedy Shopping
    [CF1119F] Niyaz and Small Degrees
    [ARC101C] Ribbons On the Tree
    [CF1446C] Xor Tree
    11月24日 模拟赛 题解
    UOJ346
    [CF1229C] Konrad and Company Evaluation
    [CF1326F] Wise Men (Hard Version)
    学军联赛模拟 第二十七测 题解
  • 原文地址:https://www.cnblogs.com/wuhui070470/p/5755057.html
Copyright © 2011-2022 走看看