JavaScript面向对象

2018-12-04  本文已影响0人  邵毅超

JavaScript中的面向对象,最简洁的一种方式,也类似于工厂模式,直接用函数来定义对象,然后创建新的对象实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>面向对象</title>
</head>
<body>
<script type="text/javascript">
    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 lux = new Person('路西',12);
    var bob = new Person('鲍勃',18);
    lux.showName();
    bob.showAge();
    // alert(lux.showName = bob.showName);
</script>

</body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读