zoukankan      html  css  js  c++  java
  • JS面向对象——组合使用构造函数模型与原型模型

    该模型为创建自定义类型最常用的方式。

    <!DOCTYPE html>
    <html>
    <head>
        <title>组合使用构造函数模型和原型模型</title>
        <script type="text/javascript">
            //组合使用构造函数模型和原型模型——构造函数模型用于定义实例属性,原型模型用于定义方法和共享属性。    
            function Student(name,age,sex){            
                this.name=name;
                this.age=age;
                this.sex=sex;
                this.friends=["Kitty","Court"];            
            }        
            Student.prototype={
                constructor:Student,
                sayName:function(){
                    alert(this.name);
                }
            }
    
            var stu1=new Student("Lucy",10,"girl");
            var stu2=new Student("Bob",9,"boy");
            stu1.friends.push("Van");
            alert(stu1.friends);//"Kitty,Court,Van"
            alert(stu2.friends);//"Kitty,Court"
            alert(stu1.friends===stu2.friends);//false
            alert(stu1.sayName===stu2.sayName);//true
        </script>
    </head>
    <body>
    </body>
    </html>

    部分摘自《JavaScript高级程序设计(第3版)》

  • 相关阅读:
    Robot Framework + Selenium library + IEDriver环境搭建
    【转】大话测试数据(一)
    TEST DESIGN TECHNIQUES: AN OVERVIEW
    24 WHEN CAN WE STOP TESTING?
    MySql Trace
    ORGANISING THE TEST CASES
    angular教程
    vue报错一
    sort()
    时间戳
  • 原文地址:https://www.cnblogs.com/planetwithpig/p/11530867.html
Copyright © 2011-2022 走看看