Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get name of prototype object

This question just got upvoted so can update question with what I did

I solved it by iterating over the window object (or user specified object root) and when I found the correct instance I backtracked and got the name from the index. The final solution can be found here

https://github.com/AndersMalmgren/Knockout.BindingConventions

Update end

I'm planning on writing a convention over configuration template source engine for KnockoutJS / MVC. I'm started with a little client side POC and ran into a show stopper right away

My plan is use this syntax or something similar

MyApp.EditCustomersViewModel = function() {
   ko.templates.loadView(this);
};

When doing this it will check the tamplate cache or fetch the templates from server using the object name as key. The problem is I cant get the name of the prototype object, i tried this

Object.prototype.getName = function() {
   var funcNameRegex = /function (.{1,})\(/;
   var results = (funcNameRegex).exec((this).constructor.toString());
   return (results && results.length > 1) ? results[1] : "";
};

If works for objects defined like this

function MyClass() { 
}

If you add a prototype to the above object it will not work, or if you define it like this

MyApp = {};
MyApp.MyClass = function() {
};

Prototype and scoping is two musts so this is a showstopper, any ideas?

Fiddle: http://jsfiddle.net/aRWLA/

edit: The background for this is like this.

On the server you have structure like this

  • Templates\ [ViewName]\index.html
  • Templates\ [ViewName]\sub-model-template.html

on the client you will do

MyApp.EditCustomersViewModel = function() {
   ko.templates.loadView(this);
};

which will generate a ajax request with the objects name as key, which will fetch all the templates for the view in question

like image 512
Anders Avatar asked Aug 23 '12 10:08

Anders


People also ask

How do I find the name of an object?

Access the name property on the object's constructor to get the class name of the object, e.g. obj.constructor.name . The constructor property returns a reference to the constructor function that created the instance object. Copied! We accessed the name property on the Object.

How do you find the prototype of an object?

The prototype of an object is referred to by the prototype property of the constructor function that creates and initializes the object. The isPrototypeOf() method provides a way to determine if one object is the prototype of another. This technique can be used to determine the class of an object.

How can we get name and type of any object?

Use the typeof operator to get the type of an object or variable in JavaScript. The typeof operator also returns the object type created with the "new" keyword. As you can see in the above example, the typeof operator returns different types for a literal string and a string object.

What is object __ proto __?

The __proto__ getter function exposes the value of the internal [[Prototype]] of an object. For objects created using an object literal, this value is Object. prototype . For objects created using array literals, this value is Array. prototype .


1 Answers

Only hoisted functions (function someFunc() {) have a retrievable name.

Assigned functions do not, because you are not technically naming the function but creating an anonymous function and assigning a reference to it (in the memory) to a named variable.

So it's the var, not the function, that is named.

This makes the very idea of retrieving function names pretty much a none-starter, since in any vaguely mature pattern you'll be writing methods, not hoisted functions - and methods of course are assigned functions.

Named expressions (see other answers) are a partial workaround but these have other issues - not least lack of support in older IEs.

(Sidenote: I've long expected browser vendors to build around this such that the names of assigned functions became retrievable, but no joy yet AFAIK.)

like image 56
Mitya Avatar answered Oct 21 '22 17:10

Mitya