zoukankan      html  css  js  c++  java
  • JS原型学习笔记

    1.原型是函数对象的属性,它的初始值是一个空对象,这个prototype原型对象可以添加方法和属性。

    2.构造器对象查找属性和方法时先查找构造器后查找原型。

    3.若构造器中的属性和原型中的属性相同,构造器的优先级会高于原型。

    4.枚举属性(for-in)

    (1).数组枚举

    var a=[1,2,3];

    for(var i in a){

      console.log(a[i]);

    }

    (2).对象枚举

    var a={name:"gao",age:"18",sex:"male"};

    for(var i in a){

      console.log(i+"="+a[i]);

    }

    5.构造器对象枚举

    function Obj(name,age){   

      this.name=name;  

      this.age=age;   

      this.getInfo=function(){    

        return "age:"+this.age+",name:"+this.name;   

      }  

    }

    Obj.prototype.score=121;  

    Obj.prototype.scape="MS";  

    Obj.prototype.getSc=function(){   

      return "score:"+this.score+".scope:"+this.scope;  

    }

    var me=new Obj("gx","17");

    for(var prop in me){   

      if(me.hasOwnProperty(prop)){    

        console.log(prop+"="+me[prop]);   

      }  

    }

    6.hasOwnProperty()函数判断属性是否为构造器自身属性

    7.isPrototypeOf()方法,当前对象是否为另一个对象的原型

    8.当对prototype对象进行重写时,需重置constructor。

    爱写代码的孩子运气不会太差。 github:http://github.com/lavyun 新浪微博:http://weibo.com/u/5921186675 个人网站: http://lavyun.cn
  • 相关阅读:
    [LeetCode] Max Area of Island
    [TCP/IP] TCP数据报
    [LeetCode] Number of Islands
    [LeetCode] Binary Number with Alternating Bits
    [TCP/IP] Internet协议
    [LeetCode] Isomorphic Strings
    [LeetCode] Path Sum
    [LeetCode] Count and Say
    [学习OpenCV攻略][001][Ubuntu安装及配置]
    [国嵌攻略][038][时钟初始化]
  • 原文地址:https://www.cnblogs.com/smartXiang/p/5890000.html
Copyright © 2011-2022 走看看