Why reading undeclared variable gives reference error but undeclared object property returns undefined in javascript?

In below code, if I print this.property2 on console, JS engine prints undefined, if I replace the statement to print only property2 compiler
throws a reference error
if I declare the variable property2 then the compiler will return undefined.

function foo() {
  var property;
  console.log(this.property2)
  console.log(this.property2 === property2)
  console.log(property2)
}

foo.property="property value";
var property2; // modified to resolve reference error
foo();
console.log(foo.property);

As I have shown in the code this.property2 is equal to property2 so why javascript behaves differently for the same variable.

Coming to my question are both variables different? (this.property2 and property2) if they are different why the comparison returns true?
and if they are not different then why I have to declare property2 to get the return value and this.property2 returns values without
declaration.

am I missing anything?


Source: stackoverflow-javascript

Which Javascript object structure uses less memory?

When it comes to JavaScript objects, arrays, and memory usage, which of the below structures is more memory efficient?

aa = [ {'a':1, 'b' : 2}, {'a':3, 'b' : 4} ]
bb = { 'a' : [1,3], 'b' : [2,4] }

aaa = [ {'a':1, 'b' : 2}, {'a':1, 'b' : 2}, {'a':1, 'b' : 2}, {'a':1, 'b' : 2}, {'a':1, 'b' : 2}, {'a':1, 'b' : 2}, {'a':1, 'b' : 2} ];
bbb = { 'a' : [1,1,1,1,1,1,1], 'b' : [2,2,2,2,2,2,2] };

It seems that it is quite difficult to get accurate measures of how much RAM an object uses in JavaScript due to a variety of reasons. However, Chrome’s new memory tab appears to possibly help us track down which of the above is better.

In the case of aa and bb, it appears that aa uses less memory (are we looking at retained or shallow size)? It’s hard to tell because the creation of that array of objects appears to create other objects at the same time.

AA:

Screenshot of Chrome’s Memory for Creation of AA

BB:

Screenshot of Chrome’s Memory for Creation of BB

In the case of aaa and bbb, it seems the object size of bbb is smaller than the array of objects of aaa, but then again, the total ram for all of the entries is less in aaa than bbb, so which one is truly more efficient?

AAA:

Screenshot of Chrome’s Memory for Creation of AAA

BBB:

Screenshot of Chrome’s Memory for Creation of BBB

Developers appear to prefer working with an array of objects because it’s easier to search (myself included), but I really only want to know which structure is more efficient in terms of memory.

I am not low level enough to understand what truly happens when JavaScript objects are stored in memory. Managing memory has been abstracted due to its complexity (thanks to all who made that possible… but poor Chromium developers).


Source: stackoverflow-javascript

Uncaught TypeError: Cannot set property ‘todoText’ of undefined

Working on the Javascript objects and functions. Facing issue with it.

  var todoList = {
      todo:[],
      displayTodo:function(){
        console.log('My Todo:',this.todo); 
      },
      addTodo:function(todoText){
        this.todo.push({
          todoText:todoText,
          complete:false
        });
        this.displayTodo();
      },
      changeTodo:function(posi,newTodo){
        //this.todo[posi] = newTodo; 
        this.todo[posi].todoText=newTodo;
        this.displayTodo();
      },
      toggleComplte:function(posi){
        var todo = this.todo[posi];
        todo.complete = !todo.complete;
        this.displayTodo();
      }
    }


    todoList.toggleComplte(0)

Error I got when running.

VM1196:16 Uncaught TypeError: Cannot set property 'todoText' of undefined
    at Object.changeTodo (<anonymous>:16:29)
    at <anonymous>:21:10

Can some one suggest what is the error ?


Source: stackoverflow-javascript

Available modules in node script

why do I get different results when trying to find out more about the http module in node.js in the following to ways?

  1. If I enter the node REPL and then print the content of the http module, i.e. if I run

    me@mymachine:~> node
    > console.log(http)
    

    I get all the details of the http object:

    { IncomingMessage: { [Function: IncomingMessage] super_: {
    ...
    
  2. If I write a script file called, say, script.js containing the following single line

    console.log(http);
    

    and execute it by running

    node script.js
    

    I get

    ReferenceError: http is not defined
    

I would have expected both cases to behave in the same way – either the http module is preloaded or not. Why is there a difference? What am I getting wrong here?

I thought I could ‘fix’ this by preloading module http by running (in version 2)

node -r http script.js

Shouldn’t this preload module http and thus avoid the reference error?

Looking forward to your input!