不二如是 发表于 2017-8-7 06:22:36

技法64 (◐‿◑) 定义构造函数

http://xxx.fishc.com/forum/201705/05/225545py2j222x5z6x55n2.png

按照提示,完成代码,秀秀你的编程能力!

不许看答案,否则打屁屁



分析:
constructor构造函数是创建新对象的函数。它们定义新对象的属性和行为。将它们视为创建新对象的蓝图。

以下是constructor构造函数的示例:
function Bird() {   
   this.name = "Albert";   
   this.color = "blue";   
   this.numLegs = 2;   
}
这个constructor定义了具有name,color和numLegs属性的Bird对象,分别为Albert,blue和2。

constructor遵循以下约定:
        ◊constructor使用大写的名称定义,以区别于不是构造函数的其他函数。   

        ◊constructor使用关键词this来设置它们将要创建的对象的属性。在构造函数中,this 是指它将创建的新对象。

        ◊constructor定义属性和行为,而不是像其他函数一样返回一个值。      



I Need U:

创建一个构造函数:Dog。其name,color和numLegs属性分别设置为字符串,字符串和数字。

Dog应该有一个设置为字符串的name属性。

Dog应该有一个设置为字符串的color属性。

Dog 应该有一个设置为数值的numLegs 属性。



答案:
**** Hidden Message *****



回顾:

技法63 (◐‿◑) 使用this关键词使代码更可复用



○面试题索引贴●
如果喜欢,请订阅{:10_303:} :
HTML5 - 庖丁解牛 + JavaScript - 庖丁解牛

Ruide 发表于 2018-3-13 11:00:11

let Dog = function() {
    this.name = "Dug";
    this.color = "red";
    this.numLegs = 4;
};

向一朵朵鲜花 发表于 2018-5-5 15:49:12

3 function Dog(){
4         this.name = "Dog";
5         this.color = "red";
6         this.numLegs = 5;
7 }

tang136 发表于 2018-5-18 23:16:17

定义构造函数

Gabriel123 发表于 2018-5-29 16:01:26

function Dog()//Dog::Dog()
{
    this.name = "xiaohua";
    this.color = "red";
    this.numLegs = 4;
}

Gabriel123 发表于 2018-5-29 16:03:23

Ruide 发表于 2018-3-13 11:00
let Dog = function() {
    this.name = "Dug";
    this.color = "red";


this is function, no ';' at the end of big bracket.

dsa159245 发表于 2018-7-11 04:54:50

111111111111

a211827754 发表于 2018-9-26 10:02:54

本帖最后由 a211827754 于 2018-9-26 10:04 编辑

5 条腿 你知我知 !!

wangdxf 发表于 2019-4-9 09:56:18

{:5_102:}

jack6666 发表于 2022-10-28 23:31:35

1

喆卡 发表于 2023-8-5 11:11:26

funcation Dog(){
       name=“dog”;
       color:“black”,;
       numLegs=12,
}
页: [1]
查看完整版本: 技法64 (◐‿◑) 定义构造函数