Javascript[2]

  • prototype主要解决:函数由于使用非常多,重复执行效率低的问题。
  • 类.prototype.方法 = function(){

    }

<script>
    function Person(name,age) {
        this.name = name;
        this.age = age;
    }
    Person.prototype.showName = function () {
        alert("姓名:" + this.name);
    };
    Person.prototype.showAge = function () {
        alert("年龄:" + this.age);
    };
    var demo = new Person("张三",26);
    demo.showName();
    demo.showAge();
</script>
原文地址:https://www.cnblogs.com/bky-1083/p/6366877.html