Permalink
Browse files

walking prototype for methods and properties

  • Loading branch information...
1 parent 092e3d8 commit 280c4e25fe2a7bb1d3852ae76a62925f99d52524 @est committed Apr 25, 2011
Showing with 23 additions and 7 deletions.
  1. +23 −7 demo/complete.js
View
@@ -117,16 +117,32 @@
function maybeAdd(str) {
if (str.indexOf(start) == 0) found.push(str);
}
+ // don't use getPrototype = Object.getPrototypeOf because it can't handle strings
+ function getPrototype(obj)
+ {
+ return obj.__proto__ || obj.constructor.prototype;
+ }
function gatherCompletions(obj) {
- if (typeof obj == "string") forEach(stringProps, maybeAdd);
- // using ECMAScript5/Javascript1.8.5 reflection method, but not for strings
- else if(Object.getOwnPropertyNames)
+ if(Object.getOwnPropertyNames)
+ {
+ if(typeof obj!="string")
+ {
+ forEach(Object.getOwnPropertyNames(obj), maybeAdd);
+ }
+ while(getPrototype(obj)!=Object.prototype)
+ {
+ obj = getPrototype(obj);
+ forEach(Object.getOwnPropertyNames(obj), maybeAdd);
+ }
+ }
+ else
{
- forEach(Object.getOwnPropertyNames(obj), maybeAdd);
+ if (typeof obj == "string") forEach(stringProps, maybeAdd);
+ // using ECMAScript5/Javascript1.8.5 reflection method, but not for strings
+ else if (obj instanceof Array) forEach(arrayProps, maybeAdd);
+ else if (obj instanceof Function) forEach(funcProps, maybeAdd);
+ for (var name in obj) maybeAdd(name);
}
- else if (obj instanceof Array) forEach(arrayProps, maybeAdd);
- else if (obj instanceof Function) forEach(funcProps, maybeAdd);
- for (var name in obj) maybeAdd(name);
}
if (context) {

3 comments on commit 280c4e2

obj.constructor.prototype is not reliable, especially not without a check that sees if obj.constructor is actually set. In general, I don't really want to add more messy code to this demo — it's supposed to show a minimal autocomplete, it won't get close to being a serious IDE.

Owner

est replied Apr 27, 2011

obj.constructor.prototype is not reliable

That's why we use proto first.

it won't get close to being a serious IDE.

May I ask are there any serious self hosted javascript IDE out there? CodeMirror2 is such promising editor.

Yes, an proto isn't available on all implementations either. You might be interested in http://cloud9ide.com/

Please sign in to comment.