0

So I am currently doing this in my Js script:

var someObject = require('./stored'); 
this.makeDuplicates = function(){
    var storeDuplicates = [];

    this.addDuplicates = function(astring){
        storeDuplicates[astring] = new someObject(); 
    }

    this.printDuplicate = function(){
        console.log(storeDuplicates["hello"]);
    }
}
var input = "hello"
var newDupe = new makeDuplicates()
newDupe.addDuplicates(input)
newDupe.printDuplicate()

this will then print undefined. Why isn't this being correctly done? I would assume it would create a hash-like table where "string"->ref to object, but it doesn't seem like it. How can I go about this? Thanks!

slebetman
  • 93,070
  • 18
  • 116
  • 145
chris123
  • 177
  • 2
  • 13
  • var someObject = function(){this.x=1}; and then get someObject {x: 1} no error, not undefined, is it require('./stored') a constructor? still it's better to use var storeDuplicates = {} instead of [], since u are using as hash table, in js it's object – Josh Lin Aug 05 '16 at 02:37
  • @YanjunLin: `require()` is a function available in node.js to import other modules – slebetman Aug 05 '16 at 02:47
  • you're either not showing all the code, or the code itself is way off. as shown, "this" is probably going to evaluate to "window" (the javascript global context) vs. the prototype of Object – jamey graham Aug 05 '16 at 02:51
  • 1
    Cleaned up indentation for you. – slebetman Aug 05 '16 at 03:11
  • @jameygraham: You're wrong. Look again. – slebetman Aug 05 '16 at 03:11
  • lol...indententation - i got it (sorry!) – jamey graham Aug 05 '16 at 03:18
  • your code works fine (except that storeDuplicates should be a {} not a [] - but this doesn't stop your code from working) - if you're running this in the console, the last output you see is the **return value of** `newDupe.printDuplicate()` - which is, correctly, `undefined` – Jaromanda X Aug 05 '16 at 03:21
  • of course i know that, i mean what u actually got by require maybe not newable – Josh Lin Aug 05 '16 at 07:06

2 Answers2

1

First, if your intention is not to use a list-like thing, don't use arrays, use objects. Unlike other languages, javascript objects are extensible. That is, you can always add new methods and properties to object instances. Therefore javascript don't have hashes since objects behave in a hash-like manner. To make using objects easy javascript has object literal syntax:

var storeDuplicates = {}; // object to act as a hash

Second, remove the this from your constructor:

makeDuplicates = function(){ /*... */ };

Everything else should work as you expect them to.

See this related question for how this works in javascript: How does the "this" keyword in Javascript act within an object literal?


Now, the following is just advisory, your code will still work without them but other javascript programmers may find it unconventional.

Since constructors (in classical javascript, constructors are like classes in other languages) are just functions there is no syntactic difference between constructors and other functions. To remedy this javascript has evolved a convention where constructor names always start with capital letters:

MakeDuplicates = function(){ /* ... */ };

When a javascript programmer sees the above he will understand that the MakeDuplicates function is intended to be a constructor.

Community
  • 1
  • 1
slebetman
  • 93,070
  • 18
  • 116
  • 145
  • an Array _is_ an object, so although it's somewhat silly to do: var x = []; x['prop'] = 'something', it functions no differently than var x = {} – jamey graham Aug 05 '16 at 02:48
  • @jameygraham: Yes, but if you never intend to use the array as an array better make it an object since it would more clearly show your intention. If I were to look at a piece of code that has an array that is never used as an array, and if I know the writer is an experienced js programmer, I'd assume that there is a bug somewhere where the author have accidentally deleted code. Besides, it DOES function differently in some cases. If you `JSON.stringify(x)` you will get `"[]"` instead of `{"prop":"something"}`. Then we'll get someone asking a question here on SO why his JSON is an empty array – slebetman Aug 05 '16 at 02:55
  • i don't disagree on the point about = [] being bad. i'm still not entirely following this answer though - at the point in time "this.addDuplicates" is evaluated, "this" will point to window (and it's only evaluated once...it's not like "this" used inside some other function). the question remains unclear – jamey graham Aug 05 '16 at 03:03
  • @jameygraham: No. Look again. The `makeDuplicates` function is called with `new`. The indentation doesn't help but it's correct. – slebetman Aug 05 '16 at 03:09
0

So I just went back to my code and realized I make an extremely careless mistake. Instead of newDupe.addDuplicates(input), my actual code had newDupe.addDuplicates("anon") hence the undefined. Nevertheless, I am reading the proper use of this. .

chris123
  • 177
  • 2
  • 13