Get the name of instance as string in Javascript - javascript

Im working with Three.js and javascript.
When my code executes this:
console.log(this.scene.children[1])
I get this in my console of Chrome:
How can I get the name of the object ('WidgetsRuler') as a string?
I dont see any attribute that saves this information.

Okay I Solved it using:
console.log(this.scene.children[1].constructor.name)

I believe you should be able to use prototype to achieve this:
Object.prototype.toString
eg:
Console.Log(this.scene.children[1].prototype.toString())
Failing that, you can try constructor:
console.log(this.scene.children[1].constructor.name)

Related

Update Object Value In Handlebars

I have one java script object which is iterating through handlebars. When I write {{this.prop}} this shows me "/bla/bla".
I want to update this property so that when I use {{this.prop}} it should return /alpha/bla/bla. I am looking for client-side manipulation. How can I do the same?
Thanks,
Try using: src="'/alpha{{this.prop}}"

JavaScript: Namespace error when using xmlDoc.evaluate()

I'm trying to get an XPathResult object using the evalute() function on a xml document object:
var nodes= xmlDoc.evaluate(XMLPath,xmlDoc,null,XPathResult.ANY_TYPE,null);
even though the namespaceResolver parameter is null and I am using a very simple XML file without any name spaces declared, Firefox throws the following error on the line:
NamespaceError: An attempt was made to create or change an object in a
way which is incorrect with regard to namespaces
Where is my error? Thanks in advance!
Ok, my bad. The error was hiding in the actual XPath I used. Problem solved.

JavaScript: how to pass object value from one function to another

I am using code lines like the following in order to fetch data from an intranet website:
util.setProp(obj, "firstNameOld", $(msg).find('#fname_a').text());
Now I have another function in the same file where I want to use the above again, resp. the value of that object - currently I am hard-coding this ('Test') for test purposes:
util.setProp(obj, "firstNameNew", 'Test');
How can I pass the value from the firstNameOld object in one function to the firstNameNew object in another function ? If a solution with global variables is better here than this would work as well.
Many thanks for any help with this, Tim.
I've never used the framework that includes util But I imagine that if there is a setProp() then there has to be a getProp() or something similar.
If so, you could do something like
util.setProp(obj, "firstNameNew", util.getProp(obj, "firstNameOld"));
This also relies on the assumption that you want to copy from two properties in the same object.
If not, then pass the desired source object in the getProp() call.
My guess is that functions (or properties) are called "firstNameOld" and "firstNameNew", so the first time you get it from selector, second time you want to do the same.
Try to use the local variable like that:
var text = $(msg).find('#fname_a').text();
//
util.setProp(obj, "firstNameOld", text);
//
util.setProp(obj, "firstNameNew", text);

JavaScript json value

I have a JSON var response (See below) but I can't retreive the value of "reputation" using response.users[0].reputation. I am in Dashcode and it says in error Result of expression response.users[unknown] is not an object. What is the correct syntax?
edit: The variable is dynamically loaded from a XMLHttpRequest. A static var with same json is working.
I guess from XMLHttpRequest you recieve string but not json object, so you need parse it first in order to get json object, for example using JSON.Parse or jQuery.parseJSON and response.users[0].reputation should work.
The JSON is fine. I just checked it and "response.users[0].reputation". (Second pair of eyes and all that.)
I would be more concerned about the "unknown" in "response.users[unknown]". It doesn't look like you are requesting the 0th index of the array "users". Something's going wrong there.
Personnally when i test the syntax of your Json, it is correct :
http://www.jsonlint.com
I have just deleted the comment "//need to get its value".
Your code works for me as you have done it. Maybe you are not assigning the variable correctly (var response = {"total": 1, etc...) or have a typo somewhere

Using .onKeyUp and .onChange with Javascript DOM objects

I'm having a weird problem with some JavaScript/DOM code I've been playing with. I'm trying to assign the .onKeyUp and .onChange events/methods to a text input like so:
form.elements["article"].onkeyup = "alert('test');";
Oddly, assigning using that method is doing nothing, and I'm forced to do this manually using:
form.elements["article"].setAttribute("onkeyup", "alert('test');");
Am I missing something here? I've used the first method I mentioned before and it has worked fine.
Try this:
form.elements["article"].onkeyup = function() { alert("test"); };
Steve
You need to assign a function, not a string. For example:
form.elements["article"].onkeyup = function(){alert('test');};
The only things I know that will take a string and eval it (other than eval) are setTimeout and setInterval.

Categories