开发者

Making JavaScript object member values more readable with a map

开发者 https://www.devze.com 2022-12-10 01:17 出处:网络
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".

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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消