this.prmtr = prmtr有什么意义?

问题描述 投票:-2回答:1

在每个游戏教程中,我都会遇到以下代码声明:

function example(parameter) {
/*What does this do?*/ 
this.parameter = parameter;
//and:
this.parameterTwo = function() { /*code*/};
}

这有什么意义?

javascript parameters this
1个回答
0
投票

相当广泛的问题,但基本上,您正在制作一个函数并将数据传递给它。您可以将使用参数传递的参数设置为使用“新”关键字(通过this.)附加到所创建函数实例的变量。然后,您可以对这些附加变量进行操作,或者在我们的示例中使用函数parameterTwo,您可以使其执行某些操作,例如返回传入的内容并添加“!”。到......>

function example(parameter){
    this.parameter = parameter; 
    this.parameterTwo = function(){return this.parameter + "!";}
}
    
var game = new example("hello");
    
console.log(game.parameterTwo());
© www.soinside.com 2019 - 2024. All rights reserved.