gpt4 book ai didi

javascript - 构造函数及其变量

转载 作者:行者123 更新时间:2023-11-30 08:29:02 24 4
gpt4 key购买 nike

我试图通过一个名为 this.anh< 的变量将 agenameheight 一起调用 来自名为 person 的函数。我写列表的方式是错误的,但正确的表示法是什么?如果有多种方式,请写下来。 :)

<script type="text/javascript">

function person(age, name, height){
this.age = age;
this.name = name;
this.height = height;
this.anh = age, name, height;
}

var koolz = new person(20,"koolz",200);



document.write(koolz.anh)

</script>

最佳答案

您需要在需要的地方添加文字并连接动态值。

function person(age, name, height){
this.age = age;
this.name = name;
this.height = height;

// If you want a literal comma and space to separate the values
// then you need to concatenate them to the variables.
this.anh = age + ", " + name + ", " + height;

// Or, if the data were in an array, like this:
var arry = [this.age, this.name, this.height ];

// You could concatenate them like this:
var result = arry.join(", ");
console.log(result);
}

var koolz = new person(20,"koolz",200);
document.write(koolz.anh)

关于javascript - 构造函数及其变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40732337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com