技法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 - 庖丁解牛
let Dog = function() {
this.name = "Dug";
this.color = "red";
this.numLegs = 4;
}; 3 function Dog(){
4 this.name = "Dog";
5 this.color = "red";
6 this.numLegs = 5;
7 }
定义构造函数 function Dog()//Dog::Dog()
{
this.name = "xiaohua";
this.color = "red";
this.numLegs = 4;
} 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. 111111111111 本帖最后由 a211827754 于 2018-9-26 10:04 编辑
5 条腿 你知我知 !! {:5_102:} 1 funcation Dog(){
name=“dog”;
color:“black”,;
numLegs=12,
}
页:
[1]