zoukankan      html  css  js  c++  java
  • 原型设计模式prototype-构造js自己定义对象

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <script type="text/javascript">
    /*
     * 原型模式:
     	prototype 原型属性
     	对象的原型拥有的属性。该对象的实例对象也会同一时候拥有
     */
     function Test(){
    	
    }
    Test.age=20;
    var tt=new Test();//这样的情况age属性不会再new时候被加入哦
    alert(tt.age)//undefine
    //--------------------
     function Test(){
    	
    }
    Test.prototype.age=20;//指定原型有的属性
    var tt=new Test();
    alert(tt.age)//这样就有了值,非undefine了
    //-------------------------
    function Student(){};
    alert(Student.prototype);
    for(pro in Student.prototype){
    	alert(pro);//弹出为空,默认该对象没有不论什么属性
    }
    Student.prototype.name="黑马";
    Student.prototype.age=3;
    Student.prototype.getName=function(){
    	return this.name;
    }
    
    var stu1=new Student();
    //stu1.name="传智"//会覆盖原型属性中的值
    delete stu1.name;//删除对象实例的属性,原型属性还是存在的哦。
    alert(stu1.name);
    </script>
    </head>
    <body>
    
    </body>
    </html>

  • 相关阅读:
    区别是否是 微信浏览器 ?
    添加收藏
    函数调用那点事
    移动端返回上一页,刚需!document.referrer 详解
    蓝桥杯-乘积尾零
    printf不能直接输出string类型
    bitset的简单用法
    to_string()函数(C++)
    蓝桥杯-明码
    手写C语言字符库
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7094517.html
Copyright © 2011-2022 走看看