coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

existential operator on object properties and function arguments

Open jfis opened this issue 15 years ago • 3 comments

i noticed some inconsistency with the existential operator im not sure whether this is intentional

version: coffee -v CoffeeScript version 1.0.1

coffee:

x?
x[0]?
x['y']?
x.y?

f = (p) ->
    p?
    p[0]?
    p['y']?
    p.y?

which make this js:

var f;
typeof x != "undefined" && x !== null;
x[0] != null;
x['y'] != null;
x.y != null;
f = function(p) {
  p != null;
  p[0] != null;
  p['y'] != null;
  return p.y != null;
};

notice how object properties and arguments are treated im new to js, so i thought maybe properties and arguments cannot be undefined also, the docs suggest coffee-script only outputs === or !== (never == or !=) //see Operators and Aliases so i did some testing:

var x;
console.log(typeof x);

function f(p)
{
    console.log(typeof p)
}

f(x);

x = [];

console.log(typeof x);
console.log(typeof x[0]);
console.log(typeof x['y']);
console.log(typeof x.y);

x = {};

console.log(typeof x);
console.log(typeof x[0]);
console.log(typeof x['y']);
console.log(typeof x.y);

which outputs:

undefined
undefined
object
undefined
undefined
undefined
object
undefined
undefined
undefined

i took a crack at seeing why this is but i couldn't get it to fully build. my guess is this block in nodes.coffee

#### Existence

# Checks a variable for existence -- not *null* and not *undefined*. This is
# similar to `.nil?` in Ruby, and avoids having to consult a JavaScript truth
# table.
exports.Existence = class Existence extends Base
  constructor: (@expression) ->

  children: ['expression']

  invert: NEGATE

  compileNode: (o) ->
    code = @expression.compile o, LEVEL_OP
    code = if IDENTIFIER.test(code) and not o.scope.check code
      if @negated
        "typeof #{code} == \"undefined\" || #{code} === null"
      else
        "typeof #{code} != \"undefined\" && #{code} !== null"
    else
      sym = if @negated then '==' else '!='
      "#{code} #{sym} null"
    if o.level <= LEVEL_COND then code else "(#{code})"

im guessing if IDENTIFIER.test(code) and not o.scope.check code is false and so it enters
else sym = if @negated then '==' else '!=' "#{code} #{sym} null"

is this intentional? should such cases be coded differently? thanks

jfis avatar Mar 20 '11 19:03 jfis

The existence operator is working properly. typeof is used whenever referencing a variable that may not be defined ("in scope"). == null is used to check for null and undefined values.

michaelficarra avatar Mar 21 '11 00:03 michaelficarra

To be a little more specific: attempting to access a variable that doesn't exist is an error, but typeof will simply return "undefined" if the variable doesn't exist; arguments are already declared, so they exist whether they have been set or not, and accessing properties that don't exist simply returns undefined so it's ok to skip the typeof check there too. This is a small optimisation that lets us avoid an unnecessary string comparison at the expense of slightly confusing new people. ;)

More confusingly, the == operator in JavaScript is a little bit magical in that it does implicit type conversions that make it useless in most cases. One of these implicit conversions is that undefined == null is true. It also does some nastier conversions that we don't want: "37" == 37, "" == false", "1" == true but "true" != true and all sorts of other weird and inconsistent things. This is why CoffeeScript doesn't output == in other cases.

Most strangely, according to JavaScript, true == "1" but `true !=

erisdev avatar Mar 21 '11 03:03 erisdev

Ah I see. I guess the type check is rarely necessary. It also seems like a language inconsistency. I don't know why an undefined variable would not be treated as if it were an undefined property of some implicit root object. Anyway, that's not a coffee-script issue. Thank you, both.

jfis avatar Mar 21 '11 04:03 jfis