JavaScript 中“类”的简单备忘

JavaScript 中“类”的实现,体现Object Oriented思想,供初学者备忘。

<head runat="server">

    <title></title>

    <script type="text/javascript">

    // 定义类  

    function MyClass(name, age) {
            this.name = name;
            this.age = age;
    }
    MyClass.prototype =
    {
        name: String , //变量
        age: Number,
        show: function() {   //方法
            alert("the name is "+ this.name);
        },

        test: function() {
            alert("the age is "+ this.age);
        }
    }

   // 使用类

    function MyGet() {

        var myInstance = new MyClass("Sophie", 4);
        testPassParam(myInstance );

    }
    // 类为参数
    function testPassParam(param) {
        param.show();
        param.test();
    }
     
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input id ="btn" type="button" value="click me" onclick="MyGet()" />
    
    </div>
    </form>
</body>
</html>
原文地址:https://www.cnblogs.com/simonhaninmelbourne/p/1425386.html