Javascript中有类吗,有的话是怎么定义的

车建涛 2019-12-21 23:35:00

推荐回答

 /**  * 自定义类   * @returns {undefined}  */    function Pepole {        var name;//名字        var age;//年龄        var gender;//性别    }    /**     * 调用方法示例:new一个Pepole类     * @returns {createPepole.pepole}     */    function createPepole {        var pepole = new Pepole;        pepole.name = "张三";        pepole.age = 22;        pepole.gender = "女";        return pepole;    }      希望参帮到你。
辛国斌2019-12-22 00:01:03

提示您:回答为网友贡献,仅供参考。

其他回答

  • 1,工厂方式:也可以带参数的!每次都要创建新函数showColor,其实可以在工厂函数外定义该函数,每个对象共享了同一个函数,然后用o.showColor=showColor;指向该方法2,构造函数方式functionCarsColor{this.color=sColor;this.showColor=function{alertthis.color;};}varo1=newCar"red";3,原型方式4,混合的构造函数/原型方式functionCarsColor{this.color=sColor;this.drivers=newArray"mike","sue";}Car.prototype.showColor=function{alertthis.color;};varo1=newCar"red";o1.drivers.push“mat”;5,动态原型方法functionCarsColor{this.color=sColor;this.drivers=newArray"mike","sue";}iftypeofCar._initialized=="undefined"{Car.prototype.showColor=function{alertthis.color;}Car._initialized=true;};可以确保该方法只创建一次6,混合工厂模式建议不采用。
    龚小英2019-12-22 00:38:12
  • js中let和var定义变量的区别,主要体现在作用于的不同。var定义的变量是全局变量或者函数变量。let定义的变量是块级的变量。例如:while1{letlet1=2;varvar1=2;}alertlet1;//不可访问alertvar1;//可以访问也就是说,let只对它所在的最内侧块内有效,而var的范围至少是一个函数之内。
    龙小翠2019-12-22 00:21:49
  • JS函数的定义方式比较灵活,它不同于其他的语言,每个函数都是作为一个对象被维护和运行的。先看几种常用的定义方式:functionfunc1;第四种是声明func5为一个对象。再看看它们的区别:?1234567functionfunc{//函数体}//等价于varfunc=function{//函数体}但同样是定义函数,在用法上有一定的区别。12345678?1234567891011121314用同样的方法可以去理解第三种定义方式。第四种定义方式也是需要声明对象后才可以引用。
    堵文波2019-12-22 00:10:35
  • javascript是一个“基于对象”的编程语言,不是面向对象的编程语言。你要知道javascript中的function定义的函数实际上就是Function对象实例。例如:functiondemox{alertx;}实际上等价于:Functiondemo=newFunction"x","alertx";所以你如果想要用javascript来模拟面向对象编程,那么就可以用function来模拟Class,用function的原型prototype或者嵌套function来模拟类的方法或者属性。下面给你一个简单的例子://模拟学生类,可以带参数,例如initNamefunctionStudentinitName{varname=initName;//模拟学生类的属性name}Student.prototype.printName=function{//定义Student类prototype中的printName函数alertthis.name;}测试时可以这样写:varstudent=newStudent"张三";//创建一个“Student对象”,实际上是一个Function实例对象student.printName;//执行student对象的printName函数。
    樊成钢2019-12-21 23:42:03

相关问答