I am relatively new to JavaScript and still trying to get my head around the prototypal inheritance. while trying to understand it I tried getting prototype property of Object object using Object.prototype in web console as well as in nodejs console. In web console it shows that the prototype property of Object object has an object with few members while in node the Object.prototype has an empty object. So my question is "is the Object object in node (console) different than the one in the web console because the are different javascript engines??"
When you ask for Object.prototype in node console, it will simply show an empty object. This is merely a representation of the base object. It still contains all the functions and properties you would expect and that show up in the browser console.
If you don't believe me, you can verify it yourself. Try typing Object.getOwnPropertyNames(Object) or Object.getOwnPropertyNames(Object.prototype) in your node console.
No, Object is the same in all implementations, the difference is just from the ways of representing it in the console.
Related
We are using the V8 version 5.3.332 in my Android App. We are trying to debug the javascript using the Node-Inspector(0.11.3)(Also tried with Eclipse Chrome Dev Tools) but strangely values of certain object properties are not shown when we hover or seen it in scoped variables section. This behaviour is particularly seen for the V8 objects which has Named Property handler attached. If the V8 object is prepared with SetAccessor then values of all properties are shown properly.
We observed that, For Named Property objects, its NamedGetter is not called for its properties so all properties are being shown as undefined.
We are preparing the V8 object as below
Local funTem = FunctionTemplate::New(isolate,callback);
Local inst = funTem->InstanceTemplate();
inst->SetNamedPropertyHandler(NamedGetter,NamedSetter,NamedQueryCallback,0,NamedEnumerator);
When Debugger tries to evaluate the expression of this object, only its NamedEnumarator is called but not its NamedGetter.
Can you please let me know what could be the issue here.
For reference, this was also asked and answered on the mailing list: https://groups.google.com/forum/#!topic/v8-dev/wTYrzdx4Y3Y.
Back in the day, I could do Array.prototype and it would show me the properties of the prototype member of the Array object. Even doing console.dir(Array.prototype) does not work in Node, however, it does work in the browser. Is there a new API that can show me the properties?
This should do it for you. Browser consoles usually have interactive displays for objects. The node repl does not have the same feature, so you have to do the below to get a similar effect.
Object.getOwnPropertyNames(Array.prototype)
I was reading on the differences between the __proto__ and the prototype object, but when I tried to access the __proto__ of an instance of an object, it returned me undefined.
Following is the code I wrote:
function Student() {
}
var student = new Student();
student.constructor // works well returns function Student() {}
student.__proto__ // returns undefined.
I was referring this blog but I saw other blogs too that show the same. We never get the prototype on the instance of an object but __proto__ object instead that was created using the prototype property.
Am I missing something or __proto__ has been removed entirely? I've tested this on Chrome version 40.0.2214.94 on Linux.
Any help appreciated. Thanks!
The property __proto__ is not standard in JavaScript versions below ECMA 6 so you cannot expect every broser to support that, or an equal behavior in every browser yet.
Its best to avoid using proto at all.
You can read about it here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto
And here is an interesting comment about that by brendan eich:
https://brendaneich.com/2011/01/harmony-of-my-dreams/
EDIT:
Object.setPrototypeOf is not going to happen. Writable proto is a giant pain to implement (must serialize to cycle-check) and it creates all sorts of type-confusion hazards. You may think you want it as a low-level sharp instrument. JS is not that language. Higher-level forms for classes and mixins seem much better and do not involve such sharp edges.
In the Node.js REPL, if you type String.prototype, an empty object: {} is returned. But if you type it in the Chrome JavaScript console, an object is returned with the expected function names and functions.
Why doesn't Node.js exhibit this behavior? How can I access the native String functions in Node.js?
According to the IRC users on FreeNode/#node.js
BennyLava: Object.getOwnPropertyNames(String.prototype)
jmar777: because in the REPL you basically get the result of calling toString() on the result, whereas the chrome console has some fancy interactive display of objects
BennyLava: they're just not enumerable
So the answer is Object.getOwnPropertyNames(String.prototype).
You could use node-inspector to get the Inspector experience for Node.
What objects are created initially by compilers(?) of javascript?
I've been learning Io in order to understand prototyping languages. After doing a bit of research I've found the javascript 'Global Object'. What I can't seem to wrap my mind around is where the other built-in functions/prototypes/objects are coming from.
There is a print object and I have no clue where it was created. Was it created by the v8 engine I am using to run the javascript code?
And similarly, I'm a bit confused as to which objects are created in a browser initially. I understand that the browser creates a dom in javascript. For example, the document object. But what other objects are there?
Also, in Io it is possible to view all objects that have been allocated memory. This is accessed through the Lobby. Is there something similar in javascript?
My favorite reference on javascript in a browser, global objects and DOM objects is MDN.
The browser creates a whole bunch of objects and makes them available for javascript access. They are created by the browser (not by the javascript engine as they aren't officially part of javascript), but the browser makes them accessible from javascript.
For example, the browser creates a document object, a window object which serves as the global object in the browser and adds a whole bunch of properties to the window object.
You can see a list of enumerable properties on the window object in your particular browser from this sample app: http://jsfiddle.net/jfriend00/nh39F/
Javascript, by itself, has some objects is creates just for it's own management of functionality. For example, there is usually a Math object that contains a bunch of math methods and a Date object that contains a bunch of date functionality.