Blogger Information
Blog 34
fans 1
comment 1
visits 40911
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
利用构造函数创造对象,向构造函数的prototype原型属性添加成员,实现数据在实例间共享——2019年7月10日22时19分
嘿哈的博客
Original
733 people have browsed it

构造函数,专用于创建对象

首字母要大写,this 代表通过构造函数要创建的新对象

实例

  var Createboj = function (){

        this.placeholder = '大于六十分以上为及格,低于六十分则为不及格';
        this.grade = function (value){
            var res = '' ;
            if(value >= 60){
                return res = '成绩合格';
            }else{
                return res = '成绩不合格';
            }
        };
        return this ;
    };
    var obj1 = new Createboj();
    console.log(obj1.placeholder);
    console.log(obj1.grade(90));

运行实例 »

点击 "运行实例" 按钮查看在线实例

效果如下:

360截图20190712161229085.jpg

构造函数的prototype原型属性添加成员,实现数据在实例间共享

实例

<script>
    var Createboj = function (){

        this.placeholder = '大于六十分以上为及格,低于六十分则为不及格';
        this.grade = function (value){
            var res = '' ;
            if(value >= 60){
                return res = '成绩合格';
            }else{
                return res = '成绩不合格';
            }
        };
        return this ;
    };
    var obj1 = new Createboj();
    console.log(obj1.placeholder);
    console.log(obj1.grade(90));
    //向原型对象添加成员,实现数据在实例间共享
    Createboj.prototype.age = '我十九岁了';
    console.log(obj1.age);

</script>

运行实例 »

点击 "运行实例" 按钮查看在线实例

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