使用映射使 JavaScript 对象成员值更具可读性
所以我有这个 Omniture 对象。它被称为s。
s
在 s 内部,我们在“props”和“eVariables”内部跟踪一堆信息。
s.prop5 = 'foo'
s.prop22 = 'baz'
s.var6 = 'bar'
我们选择分配哪些 prop 变量和哪些 evars,取决于我们正在跟踪的页面。 例如,在主页上,我们可能希望跟踪 prop5、prop6 和 evar2,但在注册页面上,我们可能希望跟踪 prop4、prop5、prop9、prop10、evar4、evar5。它有所不同。
每个变量和每个道具都代表某种关键的分析信息。
现在,尽管这个解决方案并不理想,因为 prop#s 可以全部混合在一起,但我们确实有一个内部保留的主列表,解释哪个变量代表什么。
prop5 表示“页面名称”
prop6 表示“页面类别”
(等等)
现在,这很好,而且效果很好,但我们经常必须将代码传递给第三方,以便他们可以自己分配值。我们可能让第三方创建一个页面,我们想要对其进行分析,但我们需要他们能够获取适当的信息来跟踪。为了使其更具可读性,我们正在考虑实现一些映射代码。
companyName.pageName = 'This is the page name'
companyName.contentType = 'This is the content type'
companyName.campaignId = 'This is the campaign ID'
这更具可读性。然后,我们将循环遍历“companyName”对象,并在适当的情况下将每个值分配回“s”。
你们觉得怎么样?这是一个好的做法吗?
So I have this Omniture object. It's called s.
s
Inside s, we keep track of a bunch of information, inside "props" and "eVariables".
s.prop5 = 'foo'
s.prop22 = 'baz'
s.var6 = 'bar'
Which prop variables and which evars we choose to assign, depends on which page we're tracking.
For example, on the homepage, we may wish to track prop5, prop6, and evar2, but on the registration page, we may wish to track prop4, prop5, prop9, prop10, evar4, evar5. It varies.
Each variable and each prop represents some kind of key analytics information.
Now, even though this solution is not ideal, because the prop#s can all blend together, we do have a master list that we keep internally, explaining which variable represents what.
prop5 means "page name"
prop6 means "page category"
(et cetera)
Now, this is fine, and it works well enough, but we often have to pass the code off to third parties so they can assign values themselves. We might have a 3rd party create a page, and we want to do analytics on it, but we need them to be able to get the appropriate information to track. To make it more readable, we were considering of implementing some mapping code.
companyName.pageName = 'This is the page name'
companyName.contentType = 'This is the content type'
companyName.campaignId = 'This is the campaign ID'
This is more readable. We would then loop through the "companyName" object, and assign every value back to 's' where appropriate.
What do you guys think? Would this be a good practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
老实说,我不明白为什么你首先要使用神秘的属性名称。为什么不使用您在内部提供给第三方的名称呢?这难道不会让你的生活变得更轻松吗?
Honestly I can't see why you would use the cryptic property names in the first place. Why not use the names you would give to 3rd parties internally as well. Wouldn't it just make your life easier?