-2

i'm making a javascript mod (name: xi). one of the first things im making in xi is is_true, an event type. here is how it is used:

event(is_true(insert boolean), function(){
});

function:

if (eventType == is_true(// "any" selector)) {
}

i need an "[any value]" selector, kinda like the one in css except it returns any value.

I cannot understand this stuff

well, say that the css selector * existed in javascript and it behaved like this:

typeof * // Returns "Any"

and if List() would be a thing, List(*) could return:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, ... Infinity, "a", "b", "c", "d", "e", "f", ... true, false, ...]

so have you found something similar to it? <hr> time for a q&a!!!

question number one: when will is true return false is_true kills 99.99% boolean (is_true is not boolean but it involves something like wait until [boolean here])
but anyways,

// if is_true() was a boolean
is_true(not(true)) // javascript ! is xi "not"
// D U H .

or

is_true(false)

D U H

WAIT NEVER MIND

there is a solution

var Any;

and if you need multiple "any"'s, u just gotta do

Any
Any_ii
Any_iii
...

and

// kills 99.99% boolean
event
Community
  • 1
  • 1
anova01
  • 93
  • 1
  • 10
  • Under what conditions will `is_true` return false? With which arguments? – Evert Jun 15 '20 at 22:27
  • 1
    If you want to return true for anything, just return true... – Idos Jun 15 '20 at 22:28
  • Javascript considers 0, undefined, null, false, "", and NaN to be falsey. Everything else is truthy. This means that for any value of a variable named ointments, `if (ointments)` is valid. It will be false for the six listed values and true for anything else. If you want it to be true for the top six values as well, do as @Idos sad and just return true. – Charlie Bamford Jun 15 '20 at 22:34
  • https://stackoverflow.com/questions/27321672/listen-for-all-events-in-javascript – StackSlave Jun 15 '20 at 23:22

1 Answers1

3

is_true feels like a code smell, and a bad idea.

After all, if something is true... you don't need to wrap it in a function to check this. If the goal of is_true is to check is something is, well, not false... then these two should be the same:

if (is_true(something))
if (something)

When you describe 'any value', including false, then is_true() is kind of a bad name. You don't want is_true() to return true when you call it with is_true(false) because that just gets extremely confusing.

When a variable exists, it has a value. Maybe the only type of value that you could possibly describe as 'the absence of a value' is the undefined keyword. In that case you are still better off making this explicit by letting your users do:

if (x !== undefined)

instead of something like:

if (is_defined(x))

However, my sense is that you really just need

if (x)

and no extra functions

Evert
  • 93,428
  • 18
  • 118
  • 189
  • You might want to note that if the variable or argument has not been declared you would have to test like `if(typeof x !== 'undefined')` or the Browser will throw an Error. If you were testing for an undeclared property on an existing Object, then that first type of undefined test is fine. Just a comment. – StackSlave Jun 15 '20 at 23:16
  • is_true() is not a boolean, it is event type – anova01 Jun 15 '20 at 23:30
  • also i needed the any value thingy for the script that defined ```event()``` – anova01 Jun 15 '20 at 23:58
  • 1
    @anova01 nothing you're saying really makes sense, I'm sorry. Maybe your naming needs to be better? – Evert Jun 16 '20 at 01:15
  • 1
    Your edits also... impossible to understand. – Evert Jun 16 '20 at 01:16
  • sorry i'm a weirdscratch'r :/ – anova01 Jun 16 '20 at 03:38