Blogger Information
Blog 8
fans 0
comment 0
visits 5138
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
对象基础与dom操作七月十号作业
珍珠宝宝的博客
Original
540 people have browsed it

1. 写一个构造函数来创建对象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>构造函数创建对象</title>
</head>
<body>
<script>
    var CreateObj = function() {
        this.domain =  'baidu***';
        this.get =  function(value) {
            var site = '百度: ';
            return site + value;
        }
    };
    var obj1 = new CreateObj();

    console.log( obj1.domain );
    console.log( obj1.get( obj1.domain) );

    var obj2 = new CreateObj();
    console.log( obj2.domain );
    console.log( obj2.get(obj2.domain) );


</script>
</body>
</html>

结果如下图

1564645865(1).png

2. 向构造函数的prototype中添加成员,实现数据在实例间共享

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>向构造函数的prototype中添加成员,实现数据在实例间共享</title>
</head>
<body>
<script>
    var CreateObj = function() {

        this.domain =  'php.cn';
        this.get =  function(value) {
            var name = 'php中文网: ';
        }
    };
    var obj1 = new CreateObj();

    CreateObj.prototype.site = 'baidu***';
    CreateObj.prototype.hello = function() {
        return '百度一下';
    }

    console.log( obj1.site );
    console.log( obj1.hello() );

</script>
</body>
</html>

如下图

微信图片_20190801170322.png


。。。

Correction status:qualified

Teacher's comments:前端已经学完了, 如果仍有未完成的作业, 建议先放放, 跟上当前学习进度, 双休日再补前端作业
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post