1. 主页
  2. 文档
  3. JS
  4. 面向对象
  5. 在prototype上添加方法

在prototype上添加方法

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="box">
        <p>1111</p>
        <p>2222</p>
        <p id="a3">3333</p>
    </div>

    <div id="box1">

    </div>

    <script>
        function ren(name, age, sex) {
            this.name;
            this.sex;
            this.age;

            this.shuohua = function() {

            }
            this.zoulu = function() {

            }
        }
        //原型属性。
        ren.prototype.gongzuo = "diangong";

        //原型方法。
        ren.prototype.shuijiao = function() {
            console.log(this.name);
        }

        var nren = new ren('wulei', 30, '男');

        //原型链查找
        console.log(nren.gongzuo);
    </script>
</body>

</html>
这篇文章对您有用吗?

我们要如何帮助您?