1

Consider the classic getter/setter implementation:

var test_obj={
   get num(){return this._size},
   set num(val){this._size=val;}
};
var obj1=Object.create(test_obj);
var obj2=Object.create(test_obj);
Object.getOwnPropertyNames(obj1); //[]
Object.getOwnPropertyNames(obj2); //[]
obj1.num=5;
obj2.num=7;
console.log(test_obj.num);  //undefined
console.log(obj1.num);  //5
console.log(obj2.num);  //7
Object.getOwnPropertyNames(obj1); //["_size"]
Object.getOwnPropertyNames(obj2); //["_size"]

The above is completely comprehensible to me since, for example, the value of getter obj1.num is stored in data property obj1._size and the value of getter obj2.num is stored in data property obj2._size (for both objects obj1 and obj2, _size is an own property).

My problem is I cannot find the equivalent for built-in getter/setter properties. There are plenty of them, let's pick id which is a getter/setter of Element.prototype.

var elem=document.getElementById('mydiv');

elem is an instance object of HTMLDivElement interface object, the prototype chain of elem includes Element.prototype and now we have:

elem.id; //"mydiv"
Object.getOwnPropertyNames(elem); //[]

Where elem.id's value is stored since elem does not have any own properties? In our example, it is _size that does this job. Maybe, there are some hidden properties upon elem which are not accessible? However, I cannot find any way in Javascript to hide an object property.

Thank you

Unknown developer
  • 5,414
  • 13
  • 52
  • 100
  • 1
    `elem` is a host object. Which means the implementation can do whatever it wants. In this case, the id is stored in the underlying DOM object (the real one, not just the JS proxy) – Bergi Nov 27 '15 at 09:34
  • Could you, please, be more analytical, provide more details? – Unknown developer Nov 27 '15 at 09:40
  • Not really. In general, when it comes to the DOM, you're leaving the realms of JavaScript, and getting into implementation-specific techniques. It's not a "javascript way" that hides the property - because it's stored in a non-javascript object. – Bergi Nov 27 '15 at 09:42
  • I think the duplicate answers your question well. See also [What is the difference between native objects and host objects?](http://stackoverflow.com/q/7614317/1048572) for what I meant – Bergi Nov 27 '15 at 09:48
  • Ok, something last: elem does not point to the real DOM object? What is a JS proxy? Thank you very much. – Unknown developer Nov 27 '15 at 09:57
  • `elem` is some kind of js object (accessible from JS) which proxies property accesses to the underlying "real" DOM object (typically implemented in C++ or something). – Bergi Nov 27 '15 at 10:00

0 Answers0