I thought this would be something simple to do, but I don't think it's possible, why is that?
This is what I'm trying to do.
console.log("hello") ? bool : "";
This gives me an error: Expected an assignment or function call and instead saw an expression. Not sure exactly what that means.
From my understanding, you want to "conditionally" console.log via the tenery operator.
It is possible to conditionally (via ternary) change the output of console.log() based on a bool like so:
console.log(bool ? "hello" : "")
In this case though, it will output an empty string to the console.
My guess is that you only want to log if the bool is true. It's quite trivial to add a quick if statement:
if (bool) console.log('hello')
Via Ternary, if you ultimately wish to do this, you'd need to do something like the following:
bool ? console.log("test") : null;
In my opinion, this looks less cleaner than a simple bool statement as you'd need to provide some value (Simplest probably being null or 0) which is then assigned to nothing.
Yes. you have to evaluate the expression at runtime.
Do I like this.
const someBool = true;
console.log(someBool?'hello':'goodbye');
Related
I'm working on someone else's code and I don't fully understand it yet. Besides, I'm fairly new to AngularJS.
They have a class that looks like this:
return vm.item[vm.function](request).$promise.then(function (data) {
...
});
When my vm.function has no parameters, it works perfectly. I just do
vm.function = "myFunction"
and myFunction gets called as expected.
However, now I need to call a function that has an "id" parameter. I have already tried
vm.function = "myFunction(0)"
vm.function = "myfunction({id:0})"
And even adding an "id" element to the "request" var, but no result so far.
How can I make this work?
vm.function is simply the name of the function, it says nothing of the parameters. Let's break down what's going on here.
vm.function = 'myFunction';
vm.item[vm.function](request)
translates to
vm.item['myFunction'](request)
which is equivalent to
vm.item.myFunction(request)
As you can see, it's already passing a request parameter. To add another, you'd simply do:
vm.item[vm.function](request, id)
Keep in mind when using this pattern that your dynamic function signatures must be compatible. If they begin to diverge (as you say, you're editing someone else's code) you should refactor to handle this in a different way.
In our current project we started using angularjs.
We're facing the choice to either use == rather then === everywhere or specifically parse query parameters to their respective correct type.
Hypothetical example:
//someurl.com/search?filter=astring&sortColumn=2
$scope.filter = $location.search().filter;
$scope.sortColumn = $location.search().sortColumn;
if($scope.sortColumn === 2){
doStuff();
}
Because sortColumn comes out of the querystring as a string, the strict equation fails.
I think it comes down to either use non-strict equation or parse strings to ints.
Currently we have a stringToInt() that uses parseInt() with some extra checks on null and undefined, but this feels like we're trying to write C# in js to me.
Can we get ints from search() somehow?
Or should we switch to non-strict in our equations?
Or is there another way we didn't think of?
I have one very noob question for AngularJS expressions.
I want to check in expression (ngIf in this case) that something is
undefined. Eg:
data-ng-if="typeof obj.property == 'undefined'"
How I can write expression to check this?
I would be very grateful for any ideas and recommendations.
Best regards.
An undefined property will always be falsy, so you don't have to check it specifically unless you only care that it is undefined. A simply check could then be
data-ng-if="!user.email"
That would evaluate to true unless the user object had the property email with non-falsy value (0,'',undefined are all false). Maybe that is what you are after.
You can do this by using the negation operator:
data-ng-if="!(typeof obj.property == 'undefined')"
Also your call to typeof is a noop, it does nothing. You can simply remove it:
data-ng-if="!(obj.property == 'undefined')"
I agreee with #Matt Pileggi
but just to playing around
if('prop' in obj)
Question like in title. What is exactly happening when Write method is invoked? If I have code like this:
int [] t = new int[]{2,1};
Console.Write(t);
is there any posibility that without changing Write method parameter (without adding [0]) number 2 will be displayed? (first element of array) Will this code give the same result in different .NET framework versions?
Why this method doesn't write type of y? (System.Int32[])
No that is not really possible since you would have to redefine the toString() method of Int32[].
Console.Write(t) is simply Console.Write(t.toString()) and t.toString() is Int32[].toString().
The behavior of Int32[].toString() is to simply return the type, in this case System.Int32[].
So to answer your question, no you cannot make the Int32[].toString() return the toString() of the first index.
Is it possible to somehow refer to the value I am returning from a function? An example explains better:
CFTypeRef foo()
{
CFTypeRef valueRef = NULL;
bar(&valueRef); // fills valueRef with some data
return valueRef;
}
I thought it would be nice to rewrite this as:
CFTypeRef foo()
{
bar(&__retvalue);
}
Where of course __retvalue would be some magical token. Does this make sense? Is it possible to do that? If not, why?
This is not possible. At a low level, the return value is usually returned in a processor register, making it impossible to pass as a reference.
a) It makes sense. b) There is no such magic token. c) The "If not, why"? question is just bizarre ... Why isn't there such a magic token? Because the language designers never thought of it or, thinking of it, didn't think it was a good thing to add to the language. (Someone mentioned that the return value is usually held in a register but that's irrelevant; the compiler could generate code to load that register from an in-memory variable, exactly as happens in your current foo).
Probably not.
Maybe you could define a macro if you're looking to make the code cleaner?
You could make bar() return its argument. You would save a line of code, if that's the point.
CFTypeRef foo()
{
CFTypeRef valueRef = NULL;
return bar(&valueRef);
}