英文:
Javascript destructuring in method
问题
getAdvertisingChannelHtml
方法中的代码使用了解构赋值(destructuring assignment),并将对象属性赋值给方法的参数。这种写法是JavaScript中的一种语法糖,用于简化代码。下面是对这段代码的详细解释:
getAdvertisingChannelHtml(
{ site, adViews, clicks, conversions, conversionRate } = this
)
在这个方法中,参数部分使用了解构赋值。解构赋值允许你从传递给方法的对象中提取特定属性,并将它们赋值给方法的参数。这里的目的是从传递的对象(或者如果没有传递对象,则使用this
对象)中提取以下属性:
site
adViews
clicks
conversions
conversionRate
如果没有传递对象给这个方法,它会默认使用this
对象,也就是AdvertisingChannel
类的实例对象,这是为了处理调用方法时可能的情况。
接下来,这些属性用于构建一个HTML字符串,包含了广告渠道的信息。这个HTML字符串用于表示广告渠道的属性,如站点名称、广告浏览次数、点击次数、转化次数以及转化率。
总结一下,这段代码允许你传递一个包含广告渠道信息的对象(或者使用类的实例对象的属性),然后将这些信息用于生成一个HTML字符串。这种做法可以使代码更加简洁和可读,但最佳实践取决于你的具体需求和项目的上下文。
英文:
Can someone explain what exactly is going on in depth and if doing it this way is best practice for this line:
getAdvertisingChannelHtml({site, adViews, clicks, conversions, conversionRate} = this){
My understanding is that I am destructuring in a method and passing Object Properties in a method and making it equal to the this
object?
class AdvertisingChannel {
constructor(data) {
Object.assign(this, data);
this.conversionRate = (this.conversions / this.clicks) * 100;
}
getAdvertisingChannelHtml(
{ site, adViews, clicks, conversions, conversionRate } = this
) {
return `
<div class="site-name"> ${site} </div>
<div>Views: ${adViews} </div>
<div>Clicks: ${clicks} </div>
<div>Conversions: ${conversions} </div>
<div>Conv. Rate: <span class="highlight"> ${conversionRate} %</span></div>
`;
}
}
Tried looking at documentation and trying to find out more about what exactly is going on but was still confused.
答案1
得分: 1
该函数将解构其对象参数并将值分配给特定变量(site
、adViews
、clicks
、conversions
、conversionRate
)。如果在调用函数时没有传递参数,参数默认设置为this
。
const data = {
site: 'stackoverflow.com',
adViews: 10,
clicks: 1000,
conversions: 10,
};
const advertisingChannel = new AdvertisingChannel(data);
advertisingChannel.getAdvertisingChannelHtml();
/**
* site: 'stackoverflow.com'
* adViews: 10
* clicks: 1000
* conversions: 10
* conversionRate: 1
*/
advertisingChannel.getAdvertisingChannelHtml({
site: 'example.com',
adViews: 2,
clicks: 2000,
conversions: 20,
});
/**
* site: 'example.com'
* adViews: 2
* clicks: 2000
* conversions: 20
* conversionRate: undefined(因为尚未设置)
*/
参考:默认参数
英文:
The function will destructure its Object parameter and assign the values to specific variables (site
, adViews
, clicks
, conversions
, conversionRate
). The parameter also got set to this
by default if you called the function without passing an argument.
const data = {
site: 'stackoverflow.com',
adViews: 10,
clicks: 1000,
conversions: 10,
};
const advertisingChannel = new AdvertisingChannel(data);
advertisingChannel.getAdvertisingChannelHtml();
/**
* site: 'stackoverflow.com'
* adViews: 10
* clicks: 1000
* conversions: 10
* conversionRate: 1
*/
advertisingChannel.getAdvertisingChannelHtml({
site: 'example.com',
adViews: 2,
clicks: 2000,
conversions: 20,
});
/**
* site: 'example.com'
* adViews: 2
* clicks: 2000
* conversions: 20
* conversionRate: undefined ( because it does not set yet)
*/
Refer: Default parameters
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论