Hi for some reason I can't get this function to work.
function go(type)
{
location=document.category.example.options[document.category.example.selectedIndex].value
}
All i want to do is embed the type variable, the go function becomes undefined whenever I try to do something like
location=document.'+type+'.example.options[document.'+type+'.example.selectedIndex].value
What am I doing wrong?
instead of document.'+type+'.example.options[document.'+type+'.example.selectedIndex].value you should write:
document[type].example.options[document[type].example.selectedIndex].value
You can use document[type].example
You need to learn more about JavaScript syntax before you do stuff like that.
Here is a solution to your problem:
function go(type)
{
location=document[type].example.options[document[type].example.selectedIndex].value;
}
go('category');
It takes advantage of the fact that these two are equivalent:
a.b === a['b']
But obviously in the second part, b can be replaced dynamically since it is a string.
Related
So, you know typeof, right?
What I want to do is create a function similar to that, and for this example let's just use a simple constructorof thing:
const constructorof = obj => {
return obj.constructor;
};
The syntax for it ends up being like this:
constructorof(someObjectOrWhatever);
But I want it to be like this:
constructorof someObjectOrWhatever;
Is there a way to do this?
No, you cannot create new keywords with custom logic in Javascript. You'll either need a function call, with the logic inside the function, or you'll need to inline the logic instead of calling the function. Either way, constructorof someObjectOrWhatever won't be valid syntax.
Let's say I have a simple function like this:
function alertMessage(message){
alert(message);
}
alertMessage("Hello World");
And when I type this function's name inside Chrome's console, I get something like this.
Is it possible to get the variable type of the parameter message? In that case, I believe it should be string, since that's the parameter type for the function alert according to W3schools.
Thanks in advance
Since JavaScript is dynamically typed - the type is determined during runtime - you are not able to see the type of that variable without executing the function and calling the unary typeof operator within the function body.
Try with typeof() like:
function alertMessage(message){
alert(typeof(message));
}
alertMessage("Hello World");
There is a JS function called typeof() that you can use to determine what a variable that is already initialized is
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
If you're looking for what alert() is expecting, you should look up the docs for that function
https://www.w3schools.com/jsref/met_win_alert.asp
JavaScript is dynamically typed, as Kristianmitk mentioned. However, you can use typeof to show what it was interpreted as.
function alertMessage(message) {
alert(message);
console.log(typeof message);
}
alertMessage("Hello world");
// console output:
// string
this my very first question on stackoverflow, so please forgive me if I am not getting all the etiquette yet.
I am trying to work with a previous script written in JavaScript. There is an if conditional expression in the code that I do not understand. The reason it is confusing to me is because it has 3 arguments. I have never seen an if conditional expression like this in any language, or even in the JavaScript "if" tutorial on http://www.w3schools.com/js/js_if_else.asp. The code snippet I am working with looks like this
if (this.css(alldivs[i], "contentdiv", "check")){ //check for DIVs with class "contentdiv"
setting.contentdivs.push(alldivs[i])
alldivs[i].style.display="none"}
My question is: What does if(foo, bar, "check") mean? Is this an old deprecated string comparison function in JavaScript? Why are there 3 variables in the if conditional expression instead of 2?
What are the advantages of the previous code, compared to something like:
if (this.css(alldivs[i] === "contentdiv")
Thank you for your help.
What does if(foo, bar, "check")
When you have a conditional with comma separated expressions, only the last one matters (the previous ones are also executed, though).
Then, the code is equivalent to:
foo;
bar;
if("check") { /*...*/ }
But you have this:
if (this.css(alldivs[i], "contentdiv", "check"))
That means:
Run this.css(alldivs[i], "contentdiv", "check"), where this is an object which has a the method css (a method is a function which is a property of an object).
Check the returned value.
Maybe you will understand it better this way:
var temp = this.css(alldivs[i], "contentdiv", "check");
if(temp) { /* ... */ }
Let's break down what's happening here. In this line:
if (this.css(alldivs[i], "contentdiv", "check"))
You have two things going on.
You have a function call this.css(alldivs[i], "contentdiv", "check").
You have an if() that checks the return value from that previous function call
This would be equivalent to this expanded code:
var returnVal = this.css(alldivs[i], "contentdiv", "check");
if (returnVal) {
setting.contentdivs.push(alldivs[i]);
alldivs[i].style.display="none";
}
This:
if (this.css(alldivs[i], "contentdiv", "check")) {
Would be equivalent to this:
var temp = this.css(alldivs[i], "contentdiv", "check");
if (temp) {
So you see, it's not an if taking three parameters. It's an if with one parameter which happens to be a function call that takes three arguments.
It's hard to know what answer is going to help you, as your question is about code that doesn't appear in your sample. In this case:
if (this.css(alldivs[i], "contentdiv", "check"))
The condition is based on the return value of the call to this.css. In the sample you seem interested in:
if(foo, bar, "check")
The answer is that it's always going to evaluate as True because of the way the Comma Operator behaves in JavaScript.
I am new to constructor functions, and I have the following in a .js file I include:
// should just get the number of customers out of the passed-in object
function myCustomers(customerObj) {
this.customerCount = function(customerObj) {
return customerObj.length;
};
}
On the .htm page which includes the .js file, I have the following:
var tmpObj = new myCustomers(custObj);
alert(tmpObj.customerCount);
What I expected was the alert to give me the number of customers, such as "1", etc. Instead, the alert contains the entire text of my function as though it were a string, like this:
function(customerObj) {
return customerObj.length;
}
I'm sure this is simple, but I have had a tough time googling for an answer, since my search contains very generic words like function, text/string, method, etc. FYI, this example has been pared down from a more complicated function, to hopefully make it easier to understand the question. I have tested this simpler function with the same result.
Thanks in advance!
It looks like in your alert, you are not actually calling the function, but instead passing the function itself to alert(). Try this:
alert(tmpObj.customerCount(custObj));
You might also want to change your myCustomers object to hang on to the object that is passed into the constructor, so you don't have to pass it in again when you call the function.
You should call the method like this:
var tmpObj = new myCustomers(custObj);
alert(tmpObj.customerCount(custObj));
Note the method parenthesis ;)
I'm not expert at all in Javascript and node.js
If I want to access to a method that is contained into a string, What should I do?
Is that possible?
For instance:
function bindJS(method, path){
var js = require(path+".js");
}
and the method I'd like to get is: js.what's_inside_method
Any idea how to do that?
Thanks!
Is method a property of js? Can you use js[method]()?
The only way to do this is with eval which is potentially unsafe if your data comes from a non-trusted source (unless it's hard-coded into the code, it's an untrusted source). So, lots of red flags, but this should work:
function bindJS(method, path){
var js = require(path+".js");
func = eval(method);
func();
}