javascript 基础 js类和原型

来源:互联网 发布:淘宝开店要押金么 编辑:程序博客网 时间:2024/06/01 09:38
一个函数可以看成一个类,原型是所有类都有的一个属性,原型的作用就是给这个类的每一个对象都添加一个统一的方法<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head>    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">    <title>Document</title>    <script type = "text/javascript">    //声明一个类    function Person(name,age)    {        this.name=name;        this.age=age;    }    //使用原型给类添加方法    Person.prototype.show=function()    {        alert("我叫"+this.name+",今年"+this.age);    }    //创建两个对象    var person1 =new Person('张三',20);    var person2 =new Person('李四',23);    //调用原型里面的方法    person1.show();    person2.show();    </script></head><body>     </body></html>
0 0
原创粉丝点击