zoukankan      html  css  js  c++  java
  • 构造函数

    function Animal(name, type, barkWay) {
    this.name = name;
    this.type = type;
    this.bark = barkWay;
    }
    //注意:如果像使用正常的函数一样使用构造函数
    //构造函数中的this将不再指向新创建出来的对象(因为根本就没有创建对象)
    //构造函数中的this这个时候指向的就是window全局对象
    //当使用this给对象添加成员的时候,全部都添加到了window上

    Animal("","",function () {
    console.log("我是函数");
    }); //这是一个错误的演示
    bark();
    //        window.bark();

    //如果在构造函数中定义函数,那么每次创建对象,都会重新创建该函数
    //但是函数内部代码完全相同,就造成了资源浪费
    //为了处理这个问题,我们要让所有的对象共用一个方法
    //在构造函数外部定义好该函数,将该函数赋值给构造函数内的方法

    function studyMethod(){
    console.log("我叫"+ this.name +"Good Good Study Day Day Up");
    }


    function Student(stuName) {
    this.name = stuName;
    this.study = studyMethod;
    //方法* 8*10^20
    }

    //如果构造函数没有参数,那么在调用的时候 小括号 可以省略
    var stu = new Student("vzai");
    stu.study();

    var stu1 = new Student("范冰冰");
    stu1.study();
  • 相关阅读:
    day15 web框架和Django基础
    activiti5.22 获得mybatis sessionFactory
    activiti 视图
    activiti 任意退
    spring cloud &&spring boot
    JPA 一对多关联
    webstorm 快捷键
    web storm 破解
    Excel通用导出
    activiti 小计
  • 原文地址:https://www.cnblogs.com/vzaiBoke/p/9105283.html
Copyright © 2011-2022 走看看