Feed dictionary

var FeedDictionary = (function () {
    function FeedDictionary(feedEntries) {
        var _this = this;
        this.dict = {};
        if (feedEntries) {
            feedEntries.forEach(function (entry) { return _this.dict[entry]='hello'; });
        }console.log(this.dict)
    }
    return FeedDictionary;
}());



var lol=['lol','hi','bye','hello','hey']
FeedDictionary(lol);

So this code works fine it creates a dictionary object with object keys
lol, hi etx for testing its only value is hello.
but my question is in the original samples i learned from

[entry.tensor.id]

it says _this.dict[entry.tensor.id]=‘hello’;
but whatever i tried i onli get undefined and fail to create that object…
do i need a specific module or package to handle more layered objects?
im coding in node

I’m confused here. Maybe, I’m missing the full context?

I see two problems.

#1 Do you have access to entry.tensor.id? This isn’t something NodeJS will give to you.
There must be some library that you are missing from the course you’ve learned from.

#2 The bigger problem is that your code doesn’t work as you’ve mentioned. It doesn’t add anything to this.dict

Would you care to clarify?

a typo its suppose to be _this.dict[entry]…
i corrected it now its suppose to work

Problem #1 still remains, you are missing whatever the external code that refers to entry.tensor.id. Since no one other than yourself know where that code should come from, I can’t help with this part.

Aside from that, I want to give you some code review. (This is not related to your question and you can ignore it at your will.)

  1. Why did you wrap you function? What you wrote is essentially this, there is no difference.
function FeedDictionary(feedEntries) {
    var _this = this;
    this.dict = {};
    if (feedEntries) {
        feedEntries.forEach(function (entry) { return _this.dict[entry]='hello'; });
    }
}
  1. FeedDictionary() is supposed to be a constructor. Calling constructor without new will bind dict to global namespace. You are getting the illusion that your code is working because of the console.log you put.
var lol=['lol','hi','bye','hello','hey']
FeedDictionary(lol)

console.log(dict) // Surprise, this will work.

If you want to bind dict to an instance of FeedDictionary, you want something like this

var obj = new FeedDictionary(lol)
console.log(obj.dict)
  1. FeedDictionary() doesn’t worth being a constructor. At least for what you have right now, this is simpler:
function feedDictionary(entries) {
    var dict = {}
    entries.forEach(function(entry) { dict[entry] = "hello" }
    return dict
}
  1. Use arrow function to avoid using extra variables like _this
function FeedDictionary(feedEntries) {
    this.dict = {}
    if (feedEntries) {
        feedEntries.forEach( entry => this.dict[entry]='hello' )
    }
}

I’m not sure I understand your question, but I do see some errors in this code which could lead to undefined being returned.

// here you set the *_this* var to equal *this*
var _this = this;

// but here you reference *this* directly
this.dict = {};
if (feedEntries) {
  feedEntries.forEach(function (entry) { 
   // and here you're updating *_this.dict*
  return _this.dict[entry]='hello';
  });
}

// but now you are inspecting *this.dict*
console.log(this.dict)

This happened because you reassigned this using a similar name. Always keep your variable names distinct enough to avoid confusion.

The convention is to reassign it to self, if you plan to reassign it at all.

var self = this;
self.dict = {};
if (feedEntries) {
  feedEntries.forEach(function (entry) { 
    return self.dict[entry]='hello'; 
  });
}

console.log(self.dict)

The two code snippets that you’ve posted are identical to each other in terms of their functionality. So, if a bug existed in the 1st snippet, the 2nd snippet fixes nothing. Furthermore, the naming convention is not causing any problem in OP’s code.

They are not the same, look closer.

// reassign *this* using dangling underscore
var _this = this;

// direct reference to *this*
this.dict = {};

// update the dangling underscored variable dict obj (which doesn't exist)
return _this.dict[entry]='hello';

// but logging the direct reference to *this*
console.log(this.dict)

// Compared to...

// reassign *this* to self
var self = this;

// still referencing original *this*, through self
self.dict = {};

// update the saved *this*, through self
return self.dict[entry]='hello'; 

// logging the original *this*, through self
console.log(self.dict)

The OP is binding this to what seems like the calling context, and are reassigning it so that it keeps the proper reference. Yet, they are updating one variable while inspecting the other.

While you may think that _this is just a reference to this, and would mutate it directly, that’s not always the case because of javascript scope quirks. That’s the whole reason behind reassignment in the first place.

Also,

There is a difference. The OP’s original code was an IIFE, and therefore allows for private data, which a normal function doesn’t allow.

https://blog.mariusschulz.com/2016/01/19/use-cases-for-javascripts-iifes

Let A be outer function scope.
Let B be inner function scope.
Let C be the scope of callback in forEach.

var FeedDictionary = (function () {

    function FeedDictionary(feedEntries) {
        var _this = this; // _this = this => B
        this.dict = {}; // this => B
        if (feedEntries) {
            feedEntries.forEach(function (entry) {
                return _this.dict[entry]='hello'; }); // _this = this = B 
        }console.log(this.dict) 
    }
    return FeedDictionary;
}());

Do you get it now?

There is nothing that needs access to A.
Also, the result of IIFE is just the inner function, where the inner function uses none of its outer scope. So, wrapping it is pointless.

What you are referring to only applies in a form like this

function outer() {
     var self = this
     // some other code
     
     function inner() { //  wants to access 'this' of outer function scope.
         self.doSomething()
    }
}

If you are still in doubt, I’ll post the code that proves your code snippets are identical.

I understand that. But I agreed with you that

So I qualified it by saying:

So yes, if the example posted was the complete code, it won’t make a difference.

But now I think what he really wants to do is add a value to a non-existent, nested prop on an object.

Ok, so I think I better understand what you’re going for. You’re trying to create a nested object? Like

this.dict = {
  entry: {
    tensor: {
      id: 'hello'
    }
  }
}

But you don’t know ahead of time if the prop will exist? If so, it will depend on how your data looks to start. If you can show that, then I might be able to help you.

yeah idk
i guessed [entry.tensor.id]
is a property id on the array elements
how to put it there in first place idk
Seems like the key is the tensor.id and value is just the entry
but its hard for me to figure it out. i whits testing w array of object but id come b undefined

var lol2=[ {lol:{tensor:{ id:{name:'name'},}},}]

If you have an array of objects like

var lol2=[ {lol:{tensor:{ id:{name:'name'},}},}]

then you could merge it in like

this.dict = {...this.dict, ...entry }

if you don’t, then you have to build the access keys by checking each individual one. Can you show or link me to the example where you saw this? That will help me answer it better.

You should also reply to me or someone else, otherwise we won’t get notified.

dondepas=[ {lol:{tensor:{name:"lola", id:'harry',}},},

this worked creates an object with

[lola:{ {lol:{tensor:{name:"lola", id:'harry',}},}]
1 Like