e4x newb woes - attribute trace - javascript

Sorry if I'm doing anything really dumb, but could anybody see anything immediately wrong with the following?
var layout = new XML()
layout=
<layout color="red">
</layout>
function init()
{
post(layout.#color);
}
it returns what I assume to be a ram address rather than the value.
In the event that the software in question wants an object of type string, what is the best way to enforce that? I doubt thats the cas, but am willing to try

A string cast should work fine, e.g.:
trace(String(layout.#color));
Note that in your example there's no need to assign a new XML object to layout, since you overwrite it with the next assignment one line later.
This article is highly useful if you're just getting started with E4X.

Related

Pass Component Name as Argument and then attach method (not working?)

Maybe I'm not using the right terms/names for my searches but I have not been able to find anything on this topic. I would have thought this would be an easy thing to find. What I'm doing is passing a component name to a function and then trying to use the component's name by attaching a method to it. I have confirmed (via Dev Tools) that the name is correctly being passed but when I use the variable and attach the method the specific request does not work. If I 'hard-code' the exact component name to the method it works perfectly. This makes me think the (various) ways I've been trying to attach the method to the variable name is incorrect (see various attempts below). Can you offer some direction here? Thank you.
Passing to Function ...
const grid_name = "grid_GroupA";
console.log(grid_name); // Shows grid_GroupA
msg_max(newItem, grid_name);
Function (only listing relevant parts)
function msg_max(newItem, grid_target) {
console.log(grid_target); // Shows grid_GroupA
// grid_GroupA.data.add(newItem); // This works ...
// grid_target.data.add(newItem); // This does not work
// (grid_target).data.add(newItem); // This does not work
// [grid_target].data.add(newItem); // This does not work
// grid_target + '.data.add(newItem)'; // This does not work
Thank you ...
Edit ...
In my attempt to provide detail I hope I haven't confused the issue.
In essence, my question is if I can type this exact string
grid_GroupA.data.add(newItem);
and it works for my function, how can I place a variable with the exact string "grid_GroupA" in front of ".data.add(newItem);" and have it seen the same as the line of code that works? Maybe my lack of knowledge here is getting in the way but isn't the line of code that works just a string that is then used to 'find' the object? So, if that assumption is correct, how do I create that same string with the variable? If my assumption is wrong I am a willing learner so I will be all ears. Thank you.
I do not see how grid_target is an object. You are passing grid_name(which is a string) to the function, so grid_target will have no data property, because string doesn't have such a member.
P.S. snake_case is bad option for JavaScript, consider using cameCase instead

How to do a basic contextualization in JavaScript using nlp-compromise?

So I saw this awesome natural language processing in javascript and I wonder how can I do a basic contextualization?
Let's say for example, I want to get the time.
By doing something like this:
var word = nlp(`What's the time`)
console.log(word.match('time').found)
I get a true boolean since the time word is present. But what I wanted to do is for example
nlp('What's the time') and nlp('What time is it') the value will be true but if nlp('Time is gold') the value will be false since the user didn't ask for the time.
Is that possible with this library? Any help would be much appreciated.
It sounds like what you want to do is Intent Recognition, which is usually treated as a classification problem. This article gives an overview of one way to do it; take a look at the training data:
training_data.append({"class":"greeting", "sentence":"how are you?"})
training_data.append({"class":"greeting", "sentence":"how is your day?"})
training_data.append({"class":"greeting", "sentence":"good day"})
training_data.append({"class":"greeting", "sentence":"how is it going today?"})
training_data.append({"class":"goodbye", "sentence":"have a nice day"})
training_data.append({"class":"goodbye", "sentence":"see you later"})
training_data.append({"class":"goodbye", "sentence":"have a nice day"})
training_data.append({"class":"goodbye", "sentence":"talk to you soon"})
training_data.append({"class":"sandwich", "sentence":"make me a sandwich"})
training_data.append({"class":"sandwich", "sentence":"can you make a sandwich?"})
training_data.append({"class":"sandwich", "sentence":"having a sandwich today?"})
training_data.append({"class":"sandwich", "sentence":"what's for lunch?"})
Compromise doesn't have any features for text classification so it won't help you here.
yeah, like polm23 said, this feels like a statistical classification problem.
but, if you knew, (or machine-learned) sentence-templates that suggest the intent, you could locate them in compromise with the match syntax
//what time is..
if(doc.has('#QuestionWord time #Copula')){
return true
}
//time is fun..
if(doc.has('time #Copula #Adjective')){
return false
}

What does "function(): any{" mean

I saw this snippet here:
render: function(): any {
var thread = this.state.thread;
var name = thread ? thread.name : "";
var messageListItems = this.state.messages.map(getMessageListItem);
return (
<div className="message-section">
<h3 className="message-thread-heading">{name}</h3>
// ...
What does the function(): any{ part in the first line mean?
Apologies if this has been asked before, but it's really hard to search this, particularly when you don't know what it's called.
That's not a part of JavaScript, it's an extra feature added by Flow, a JavaScript preprocessor. TypeScript also has a similar feature.
Essentially, Flow adds a type-checking feature, and to use it you add type-hinting to symbols. In this case, : any is a type hint for the render method, meaning the method could return any type.
Excerpt from the type annotations docs for any:
any is a special type annotation that represents the universal
dynamic type. any can flow to any other type, and vice-versa. any
is basically the "get out of my way, I know what I am doing"
annotation. Use it when Flow is getting in your way, but you know your
program is correct.
A fun little side note, there was a proposed feature in the now-abandoned ES4 draft for type hinting that was very similar to this. As far as I know, it was only ever implemented in the ES-derived ActionScript 3.
Very simple and straightforward answer is function(): any will return any type of data.
It means you can return string boolean number or any type of data you want from that function.

JavaScript - case sensitivity issue within an object/property

I have an issue with JavaScript case sensitivity and I will need your valuable piece of advice here. I have the following object created:
var foo = function () {
this.myColor1 = '#000000';
this.MyColor2 = '#FF2000';
this.MyCOLOR3 = '#FFFFFF';
}
as you can see, each property may come in any case form, lowercase, uppcase, mixed, etc. These values are coming from a database and I don't have control onto them.
I want to be able to call them ignoring the case sensitivity. For example, I would like to be able to call them like this:
console.log(foo.mycolor1);
// or
console.log(foo.myColor1);
I guess my only approach to achieve this, would be to convert everything in, let's say, lowercase when I define those, and then, when I call them back to convert my request into lowercase again.
A little piece of background here; my aim is to provide an SDK to a few developers that they will write their own code for a platform I am working on. These values will be saved by the developers themselves into a database. For some reason, all those values are stored in lowercase. So, I either have to tell them 'no matter how you set them, you should request everything in lowercase', or, ideally, I should find a way to convert everything before their request is post.
An idea would be to write a method, and tell them to make the request like this
foo('mycolor1');
foo, is going to be a function that would handle the case sensitivity easily. But, I would prefer to use the foo.mycolor1 notation, so ... your help is needed :)
FYI, jQuery is available!
Thank you,
Giorgoc
when you render the javascript from DB use toLower() to set the variables names... and then reference them in lower case...

Accessing a node-set in a JavaScript XPath query

I have a real simple question that I can't seem to find an answer to.
I want to compress two XPath statements (that are getting attribute values). I learned about the | operator, hearing how it returns node sets.
var getdata = xmldoc.evaluate
(
'/foo/bar[#world=\''+hello+'\']/child::*/attribute::name
|/foo/bar[#world=\''hello+'\']/child::*/attribute::id',
xmldoc, null, XPathResult.ANY_TYPE, null
);
To anyone wondering, no I do not format my evaluation strings that way ... though, I sort of like it now that I typed it out. Anyways, this is how I tested it out.
alert(getItemData.iterateNext().childNodes[0].nodeValue);
That works! But it only returns the first one. While writing this, I just tried .length and made a break through ... it's only counting one item. Was I deceived about the concept of |? How can I get a set and then go through them?
XML document, as requested.
<?xml version="1.0" encoding="ISO-8859-1"?>
<foo>
<bar world="hello" id="1">
<subbar name="item1" id="2">
</subbar>
</bar>
<bar world="bye" id="3">
<subbar name="item2" id="4">
</subbar>
</bar>
</foo>
Edit: I am currently using a function that grabs the element rather than the attribute, but I would really like to know the other way. Unless what I am doing is the best way.
If JQuery is an option, it might be worth your while to check out their XML traversal library. A quick search pulled up an article here. I wrote up a very rough example of what the logic may look like after you import the xml document, which is explained in the link.
var hello = "foo";
$('bar[world=' + hello + '] > subbar').each(function () {
// You'd want to save these values somewhere else, obviously.
$(this).getAttribute(name);
$(this).getAttribute(id);
});
The key here is the XPathResult type you use.
I have implemented a working sample for the same.
Please refer the code at http://jsbin.com/eneso3/5/edit
Basically you have to use Iterator as result type sot hat we can iterate through them to get the text. Refer Xpath reference mentioned on the working code sample page.
Well your usage of the "pipe" is correct (http://www.tizag.com/xmlTutorial/xpathbar.php) so the only code that I can see might be off is a missing + in the second xpath command, but that might be pseudo code, so I would only count this as a half answer. As for the best practice, in my opinion I would grab the subbar element then grab it's attributes out where you need them an optimization like the one you've suggested obfuscates what data is being referenced. Seems too much of a mico-optimization, but this is just an opinion. Maybe you have a long list of attributes and you really are saving parsing time.

Categories

Resources