JavaScript - using apply to invoke function, but 'this' is not being set to the object passed as first argument -


i'm trying use apply method invoke objectify function, passing array of values , using obj object. however, this not being set object, rather global environment (window).

the purpose of array of strings, pass strings function objectify. when function invoked, takes array values, splits them, and, if object not have property string value, property created.

here code below:

let obj = {}; let arr = ["user.name.firstname=john", "user.name.lastname=smith"];  const objectify = x => { let cur = this, v; return x.split('.').foreach(e => /=/g.test(e) ? (v = e.split('='), cur[v[0]] = v[1]) : cur[e] ? cur = cur[e] : (cur[e] = {}, cur = cur[e]))};  objectify.apply(obj,arr); 

the problem this set window rather object obj. how should rewrite code sets obj this value?

the end result should modified obj object becomes:

obj = {user: {name: {firstname: 'john', lastname: 'smith'}}}; 

this (no pun intended) happening because objectify "arrow function" uses this inherits lexical scope, , ignore passed via .apply or .call.

if rewrite normal function should work desired.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -