zoukankan      html  css  js  c++  java
  • 对象原型

    如何判断一个对象的方法是来自这个本身的还是原型的?

    function Person() {
    }
     
    Person.prototype.name="Nicholas";
    Person.prototype.age=29;
    Person.prototype.sayName=function(){
        alert(this.name);
    }
     
    var person1=new Person();
    person1.name="Greg";
     
    var person2=new Person();
     
    console.log(person1.hasOwnProperty("name"));//true
    console.log(person2.hasOwnProperty("name"));//false
     
    console.log("name" in person1);//true
    console.log("name" in person2);//true
     
    for (var prop in person1) {
        console.log(prop);//name   age   sayName
    }
     
    function hasPrototypeProperty(object,pro) {//如此可判断存在于原型中的属性
        return (!object.hasOwnProperty(pro))&&(pro in object);
    }
    console.log(hasPrototypeProperty(person1,"name"));//false
    console.log(hasPrototypeProperty(person2,"name"));//true
    function Person() {
    }
     
    Person.prototype.name="Nicholas";
    Person.prototype.age=29;
    Person.prototype.sayName=function(){
        alert(this.name);
    }
     
    var person1=new Person();
    person1.name="Greg";
     
    var person2=new Person();
     
    console.log(person1.hasOwnProperty("name"));//true
    console.log(person2.hasOwnProperty("name"));//false
     
    console.log("name" in person1);//true
    console.log("name" in person2);//true
     
    for (var prop in person1) {
        console.log(prop);//name   age   sayName
    }
     
    function hasPrototypeProperty(object,pro) {//如此可判断存在于原型中的属性
        return (!object.hasOwnProperty(pro))&&(pro in object);
    }
    console.log(hasPrototypeProperty(person1,"name"));//false
    console.log(hasPrototypeProperty(person2,"name"));//true

     

  • 相关阅读:
    For each db / table
    转---网络上来的,做一个数组样的结构
    JAVA 相关资料
    转--也不知是哪位大侠写的了
    T-SQL切割字符串方法小结 2
    OPENQUERY
    行集函数专题
    行列转换
    第一章 SQL基础
    解释型语言与编译型语言的区别
  • 原文地址:https://www.cnblogs.com/yuaima/p/5901557.html
Copyright © 2011-2022 走看看