zoukankan      html  css  js  c++  java
  • hasOwnProperty与isPrototypeOf

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>hasOwnProperty与isPrototypeOf</title>
    </head>
    <body>
        <script>
        // hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象。不过需要注意的是,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员。
        // isPrototypeOf:是用来判断要检查其原型链的对象是否存在于指定对象实例中,是则返回true,否则返回false。
            function person(firstName,lastName) {
                this.firstName=firstName;
                this.lastName=lastName;
            }
            person.prototype.setName=function(){
                return this.firstName+','+this.lastName
            }
            var jone=new person('jone','chen');
            jone.age=30
            console.log(jone.setName());                          // ==>jone,chen
            console.log(jone.hasOwnProperty('setName'))           // ==>false
            console.log(jone.hasOwnProperty('age'))               // ==>true
            console.log(jone.hasOwnProperty('firstName'))         // ==>true
            console.log(person.prototype.isPrototypeOf(jone));    // ==>true
            console.log(person.prototype.isPrototypeOf('jone'));  // ==>false
        </script>
    </body>
    </html>

    hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象。不过需要注意的是,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员。

    isPrototypeOf:是用来判断要检查其原型链的对象是否存在于指定对象实例中,是则返回true,否则返回false。

    var person = function (work) {
        this.work = work;
    };
    person.prototype.name = "jone";
    person.prototype.age = 30;
    person.prototype.say = function () {
        console.log(this.name + ',' + this.age + ',' + this.work)
    }
    var jone = new person("xuesheng");
    jone.say();
    console.log(person.prototype.hasOwnProperty('name'));
    console.log(person instanceof Object);
    console.log(jone.hasOwnProperty('work'))
    console.log(person.prototype.hasOwnProperty('work'))
    console.log(person.prototype.isPrototypeOf(jone))
  • 相关阅读:
    Kotlin开发 扩展函数在Android开发中的一些实用例子
    Kotlin开发 使用lambda实现接口回调
    Android开发 Camera2的CaptureRequest属性整理--完善中
    Android开发 Bitmap图像处理详解
    Android开发 Camera预览画面镜像问题
    Android开发 ViewPage2
    windows 服务器更新程序下载-修复漏洞
    JAVA实现数据等分,一个List分成多个List
    List<CourseRecord>转HashMap<Long, List<CourseRecord>>
    模拟服务器1.0——WebServer 、ClientHandler 接收请求、做出响应
  • 原文地址:https://www.cnblogs.com/jone-chen/p/5218434.html
Copyright © 2011-2022 走看看