Can someone explain the difference in Javascript between:
var x = something
and
var x : something
I have no idea on where/how to search about it.
I saw the code above at the bottom of page 4 of this document: http://download.unity3d.com/support/Tutorials/2%20-%20Scripting%20Tutorial.pdf
Thanks in advance!
The first one assigns something to a variable x and the other causes a syntax error.
You're probably mixing up assigning a property in an object literal and normal assignment.
var x = something;//assigning a variable
var y = {
x:something//assigning a object property
};
Edit
var target : Transform;
seems to be UnityScript not JavaScript, it looks like it is not assigning a value but rather setting the variable type. see here
UnityScript is not JavaScript
Unity Script vs Javascript
If you're defining vars in an object, you'd use colons.
var obj = {x:my_var};
Related
I have a variable that contains a string with interpolated variables. In the code below, that variable is template. When I pass this variable to generateString function, I want to apply string interpolation on it because the values which interpolated variables require are available in generateString function only.
void main() {
String template = '<p>\${name}</p>';
var res = generateString(template);
}
generateString(template) {
var name = 'abc';
print(template);
return template;
}
The problem is when I am printing and returning template inside generateString fn, I am getting <p>${name}</p> instead of <p>abc</p>. Is there a way to explicitly tell the dart to so string interpolation?
I am new to Dart. I don't know if it is even possible to achieve or not. Please suggest how do I do this.
Edit: Based on the inputs from other users, I would like to make a clarification about the scenario presented. The value of template variable is not a string literal. I get that from UI as a user input. I have shown it here as a string literal for code simplicity. Also, please consider that name and template are not in the same scope in my scenario.
The other answers so far are wrong.
String interpolation (looking for $, etc) happens only while compiling from the source code to the value in memory. If that string in turn also has a $, it's no longer special.
It's not possible to trigger interpolation past the original compilation step. You can write a templating system that would look for something like {{name}} in the value, and replace it with the current value of name.
If you have the template and the variable in the same scope, it works as expected.
// evaluate variable inside ${}
var sport = 'basketball';
String template = 'I like <p>${sport}</p>';
print(template);
I didn't fully understand your question maybe this will help
void main() {
print(generateString('abc')); //<p>abc</p>
}
generateString(String template) {
return r"<p>" "$template" r"</p>";
}
Walter White here.
You must define the variable name as global var, so it can "cook" the string for you
Why I could not declare variable in setup and it still work and fine?
Where I could skip declaration, what is negative part of that skiping? - Main question
Here is example of code without declaration and it work fine:
function setup(){
canvas = createCanvas(innerWidth, innerHeight);
}
function mouseClicked(){
canvas.style('margin-top', '5px');
}
Here is same code with declaration how should it be:
let canvas;
function setup(){
canvas = createCanvas(innerWidth, innerHeight);
}
function mouseClicked(){
canvas.style('margin-top', '5px');
}
and I have also code in console that shows as it should be and I understand why it is so : declaration variables in console
It is working on any of DOM element (not only canvas)
This is a JavaScript thing. See here for more info:
Is using 'var' to declare variables optional?
What is the purpose of the var keyword and when to use it (or omit it)?
Basically, if you don't use var or let or const, then the variable is put in the global scope. In this case it doesn't really matter, since you're putting the variables in the global scope no matter what.
But it's a good idea to always use var or let or const to declare a variable, even if you don't technically need it.
I'm trying to use code for a school assignment, and I copied the professors outline like we were supposed to but one of the lines is causing an error in my ide saying missing arguments for method apply in object List; the line of code is
private var d = List[(String,Any)]
and I really don't understand what is wrong with it?
You need parens after the type to actually create the empty list:
private var d = List[(String,Any)]()
or alternatively:
private var d = List.empty[(String,Any)]
I am currently working on a struct in MATLAB and have a question regarding this.
Let us say i have declared a struct:
structVariable=struct('abc',[],'cde',[])
i.e.
structVariable =
abc: []
cde: []
Further I have a char variable,
charVariable='abc';
Now, I am trying to use structVariable.abc with something like
structVariable.charVariable =5;
but this does not work. Is it possible to reference to the value of charVariable with something like &charVariable as in c++ ?
This seems to be the easiest way:
structVariable.(charVariable) = 5;
To set the field value:
setfield(structVariable,charVariable,5)
To get the field value:
getfield(structVariable,charVariable)
I can't seem to find a concise answer to this question. What is the correct coffeescriptic way to return the value from _otherInstanceMethod when calling #_instanceMethod instead of the function binding itself?
x = _instanceMethod: () ->
#_otherInstanceMethod key: 'value'
Edit (thanks commenters)
This returns:
x = function () {
[...] # function body omitted
});
Instead of
x = 'some value returned by _otherInstanceMethod'
I would like the value to be returned instead of the function binding to _otherInstanceMethod
Being totally new to Coffeescript, this was my fault. I was calling the instance method like:
#_instanceMethod
instead of
#_instanceMethod()
Sorry for the trouble, voting to delete
In CoffeeScript #something translated into this.something regardless of the underlying variable type. This means you can use # only in conjuction with properties, with methods you still ought to use good old this.