javascript can't use input to grab an object property - javascript

So i've got this code below (all javascript). And I wish to grab the votecount for a game on user input
function Game(gamename,votes) {
this.gamename = gamename;
this.votes = votes;
};
var lol = new Game("League of Legends",1100);
var dota = new Game("DOTA 2",2100);
var ql = new Game("Quakelive",3100);
var csgo = new Game("Counter Strike: GO",4100);
function PostVotes(gnshort){
//string names - working
console.log(gnshort + 'name');
console.log(gnshort + 'votes')
var CalcVotes = function(gnshort){
var votecount = gnshort.votes;
console.log(votecount);
}
CalcVotes(gnshort);
//CalcVotes(lol); //works
};
PostVotes('lol');
I keep getting the error undefined when calling CalcVotes(gnshort). and I know it's not the function it's passing the lol as gnshort it's asif it's reading as a string instead of a variable or something. I've only been learning javascript for the past week so any advice would be helpful

PostVotes('lol'); will pass lol as a string ('lol' is equivalent to "lol"). What you need to do is simply pass the variable lol like
PostVotes(lol);
And it will return lol.votes, aka 1100.

Related

Split php varibale with JS into array

I have tried everything and I can not split a PHP variable into two parts so I can insert them into two input fields. I have read numerous topics here and I don't see the problem...
This peace of code gives me a result that php variable is inserted into a wanted filed.
Lets say the PHP variable is data1-data2:
document.hiderad.selectstate.onchange = updateText;
function updateText() {
var str = document.hiderad.selectstate;
document.hiderad.opis.value = str.value;
}
Code above inserted data1-data2 into wanted HTML input.
And soon as i try to split it i get undefined warning. I have tried 7 different things to approach this problem so i want even list all the versions I tried. Can someone please help?
document.hiderad.selectstate.onchange = updateText;
function updateText() {
var str = document.hiderad.selectstate;
var array = str.toString().split('-');
a = array[0], b = array[1];
document.hiderad.opis.value = a.value;
document.hiderad.iznos.value = b.value;
}
Code above gives me b undefined if i remove last line i get a undefined.
You shouldn't be using a.value and b.value, that's for getting the value of an input field, not a string. You should use that to get the value of the selectstate input.
Also, always declare local variables unless you have a specific reason to assign global variables.
function updateText() {
var str = document.hiderad.selectstate;
var array = str.value.split('-');
var a = array[0], b = array[1];
document.hiderad.opis.value = a;
document.hiderad.iznos.value = b;
}

Referencing, then calling, a prototype function

I am trying to make an array of functions so that I can train a machine learning algorithm. One problem I am running into, for instance:
var fun = [String.prototype.split];
var str = 'test1&test2';
var result = str.fun[0]('&');
gives me the error of "cannot read property 0 of undefined". this is because the str does not have the literal fun array within itself to be called. Is the only way to correct this is to wrap every function such as the following or is there another way:
function splitter (str1, str2) {
return str1.split(str2);
}
var fun = [splitter];
var str = 'test&test';
var result = fun[0](str, '&');
If there is another way to do this I would really like to know as it will save me a lot of time wrapping every function like the above.
var arr = [String.prototype.split];
var str = 'test&test';
arr[0].call(str, '&');
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call

JavaScript Array Accessing elements

I'm fairly new to Javascript, what I am trying to do is work out how to setup the correct array style for my form. What I want to be able to do is add each schedule to an array as a string and then be able to access the elements of that string(s) later for use such as sorting or searching for in the string. Currently its accessing each word as an index not a string first, the correct way to access an element within a string would be as myArray[0] = the string and then myArray[0][0] = the first element of that string and so on and so forth? I'd like to use vanilla Javascript as im still learning the fundamentals. Below is my code for how i am adding each schedule to the array, Im not sure where im going wrong, I think it has to do with the way its getting added to the global array. Thanks guys.
var myArr = new Array();
function addSchedule() {
var myPriority;
var date;
var endtime;
var mySubject;
if(document.getElementById('high').checked){
myPriority = document.getElementById('high').value;
}else if(document.getElementById('low').checked) {
myPriority = document.getElementById('low').value;
}
date = document.getElementById('Date').value;
endtime = document.getElementById('endtime').value;
mySubject = document.getElementById('Subject').value;
var priorityString = (myPriority)
var dateString = (date)
var subjectString = (mySubject)
// I think this needs to be a string some how but i dont know
var oneString = new Array(priorityString,dateString,endTimeString,subjectString)
myArr[myArr.length] = (oneString)
I think what you're actually trying to do is add objects to an array which represent each of the properties you're reading from your UI
var myArr = new Array();
function addSchedule() {
var myPriority;
var date;
var endtime;
var mySubject;
if(document.getElementById('high').checked){
myPriority = document.getElementById('high').value;
}else if(document.getElementById('low').checked) {
myPriority = document.getElementById('low').value;
}
date = document.getElementById('Date').value;
endtime = document.getElementById('endtime').value;
mySubject = document.getElementById('Subject').value;
// create an object
var myObj = {
priority: myPriority,
date : date,
endTime: endtime,
subject: mySubject
};
// add it to array
myArr.push(myObj);
}
Thereafter, if you wanted to read an object & it's properties from the array you could have
// for example:
var firstObjectPriority = myArr[0].priority;

Create SAPUI5 control using new Function() and string

This question is a continuation of this one.
I have a list of SAPUI5 controls in string format and to extract the required control I use:
var sDefaultControlConstructor = "new sap.m.Input()";
var sConstructor = "return " + sDefaultControlConstructor;
var oConstructor = new Function(sConstructor);
var oField = oConstructor();
The oField object I get looks like follows in the console:
M.c…s.f {bAllowTextSelection: true, mEventRegistry: Object, sId: "id",
mProperties: d, mAggregations: Object…}
The problem is that I cannot get the created object using sap.ui.getCore().byId() function.
I looked for the differences between the object I create and the object which is created "normally"
var oField = new sap.m.Input();
it looks like this:
d {bAllowTextSelection: true, mEventRegistry: Object, sId: "id",
mProperties: d, mAggregations: Object…}
Obviously, these two object are different.
The question is how do I create the control of the second format using new Function().
Thank you.
OK, I have found the solution.
I was declaring the control wrong - the id to every control is given by user when this control is being created, while when user doesn't give any id to the control, it is given automatically.
So, when I did
var sDefaultControlConstructor = "new sap.m.Input()";
var sConstructor = "return " + sDefaultControlConstructor;
var oConstructor = new Function(sConstructor);
var oField = oConstructor();
my control oField was created with automatically generated and assigned id.
And when I give id to this control afterwards -
oField.sId = "someID";
this control cannot be seen by sap.ui.getCore().byId("someID"); (I honestly don't know why. It always return undefined).
To fix the issue, its required to assign control's id during the control's creation:
var oConstructor = new Function("a", "return new sap.m.Input(a)");
var oField = oConstructor({id:'someID'});
OR
var sId = 'someID';
var oConstructor = new Function("a", "return new sap.m.Input(a)");
var oField = oConstructor({id:sId});
HERE is JSBIN example of two above declarations (working and not working ones).

Pass char * from javascript to c and return char * from c to javascript in Jsctypes

I have this C code in a library "pruebaChar.so" for use with jsctypes:
char * ejecutarComando(char * miComando){
return miComando;
}
Then, I have this other code for call "pruebaChar.so":
function miComando(unComando) {
var {Cu} = require("chrome");
var {ctypes} = Cu.import("resource://gre/modules/ctypes.jsm", null);
var lib = ctypes.open("/home/usuario/pruebaChar.so");
var comandoLib = lib.declare("ejecutarComando",
ctypes.default_abi,
ctypes.char, // return type is correct?
ctypes.char.ptr // argument type is correct?
);
/* How do I pass the argument to function
and save the return value from function?*/
var returnString = comandoLib(unComando); // Is this correct?
return ???;
}
What values should I put in return type and argument type? How do I pass the argument to function and save the return value from function?
Thank you very much :)
Instead of ctypes.char try using ctypes.char.ptr and for first argument of string it looks like it would work. which is also ctypes.char.ptr.
here's an example of someone doing a x11 jsctypes with string as return:
declaring it: https://github.com/foudfou/FireTray/blob/a0c0061cd680a3a92b820969b093cc4780dfb10c/src/modules/ctypes/linux/gtk.jsm#L138
lib.lazy_bind("gtk_window_get_title", gobject.gchar.ptr, this.GtkWindow.ptr);
this is just lazy binding but it converts to:
var blah = ctypes.declare('gtk_window_get_title', ctypes.default_abi, ctypes.char.ptr, ctypes.voidptr_t);
then he uses it like this:https://github.com/foudfou/FireTray/blob/0fee978ec353f1153865541d286fe6c8adacdf4b/src/modules/linux/FiretrayWindow.jsm#L141
let data = ctypes.cast(userData, _find_data_t.ptr);
let inTitle = data.contents.inTitle;
let gtkWin = ctypes.cast(gtkWidget, gtk.GtkWindow.ptr);
let winTitle = gtk.gtk_window_get_title(gtkWin);
if (!winTitle.isNull()) {
log.debug(inTitle+" = "+winTitle);
if (libc.strcmp(inTitle, winTitle) == 0)
data.contents.outWindow = gtkWin;
}
}
This looks to me like the returned is not a jsstring, as he's using libc.strcmp for c string comparison. so i think you would have to maybe do winTitle.readString() to get the string into your js. maybe not, im not sure, maybe a a winTitle.contents would work too.
Im not pro a js-ctypes but if you can send me your .so file and tell me a bit about i can get it to work and let you know.

Categories

Resources