Just JavaScript - The Object Expression
Written by Ian Elliot   
Thursday, 12 June 2014
Article Index
Just JavaScript - The Object Expression
String Values
Comparison Operators
Functions In Expressions

Recap

To summarize:

  1. There are two equality (and inequality) operators == and the strict equality operator ===.

  2. If the operands are both object references op1==op2 and op1===op2 work in the same way and return true only if the two operands reference the same object.

  3. In all other cases op1==op2 calls valueOf by default and toString if needed before the test and op1===op2 doesn't and just performs the comparison. 

  4. You can always force valueOf to be called by adding a unary plus + in front of the operand. Thus +op1===+op2 is almost the same as op1==+op2, but it doesn't automatically call toString if type conversion is required. 

 

Functions in expressions

As well as objects you can also include function evaluations within an expression. For example

var a=2*myFunction();

in this case the function can return a general object and the valueOf and toString methods are used as described earlier to obtain a primitive value of the correct sort.  For example if myFunction returns a custom object then its valueOf method is called to obtain a primitive value etc.. 

What is interesting is that as a Function object is an Object it too can define valueOf and toString methods. So for example:

var myFunction=function(){return 1;};
myFunction.valueOf=function(){return 2};

Now you can write

var a=2*myFunction();

and the result is 2 or

var a=2*myFunction;

and the result is 4.

You could also put a function evaluation in the valueOf method so that the function can be called with or without () and parameters. 

myFunction.valueOf=function(){return this()};

Now writing myFunction() returns the same as writing myFunction. 

The object expression principle

Now that we have all of the fine detail of how object expressions work, it is time to state the key principle of using expressions in JavaScript - the Object Expression Principle:

   Anywhere you can use an object you can use an expression

At this point you might be thinking that this is not particularly useful as expressions evaluate to primitive values - Number, String or Boolean. However, as you can use a function in an expression, a single function evaluation is also an expression and in this case the return value can be any object.

This one simple idea makes it possible to do a lot of things that would otherwise be very difficult. 

For example when you are defining an object literal you can write:

var myObject1={myProperty:myObject2};

which sets myProperty to be a reference to myObject2. 

However, if you  write

var myObject1={myProperty: +myObject2};

then myProperty will be set to whatever myObject2.valueOf returns.

And finally 

 var myObject1={myProperty: myObject2()};

sets myProperty to whatever the function body of myObject2 returns.

Of course this last one assumes that myObject2 is a Function object. 

The ability to use an object expression anywhere an object can be used might seem obvious or a small thing - but in practice it turns out to be a very powerful idea that makes a lot of interesting things possible. Often the use is so commonplace that you might not even have noticed that this is exactly what is going on.  

 

 

JustJavaScripticon

 

Just JavaScript 

 There is a newer version of the draft of the book here.

A Radical Look At JavaScript

Contents

  1. JavaScript Isn't Java, or C, or C# ... (Book Only)
  2. In The Beginning Was The Object
  3. The Function Object
  4. How Functions Become Methods
  5. The Object Expression
  6. Object Construction
  7. The Prototype
  8. Type And Non-Type
  9. Constructor And InstanceOf
  10. Duck Testing And Prototype Construction

-Preface-

Most books on JavaScript either compare it to the better known class based languages such as Java or C++ and even go on to show you how to make it look like the one of these.

Just JavaScript is an experiment in telling JavaScript's story "just as it is" without trying to apologise for its lack of class or some other feature. The broad features of the story are very clear but some of the small details may need working out along the way - hence the use of the term "experiment". Read on, but don't assume that you are just reading an account of Java, C++ or C# translated to JavaScript - you need to think about things in a new way. 

Just JavaScript is a radical look at the language without apologies. 

 

 

Coming Next

More functions - Object factories and constructors

Related Articles

Javascript Jems - First class functions

JavaScript Objects With Value - valueOf and toString

JavaScript Hoisting Explained       

WAT! JavaScript, Ignorance And Prejudice       

The Undefined Defined Variable       

Javascript Jems - Active Logic, Truthy and Falsey

Objects with Values in JavaScript

Impossible Equalities - a JavaScript puzzle

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

 

Banner


JavaScript Jems - The Inheritance Tax

JavaScript should not be judged as if it was a poor version of the other popular languages - it isn't a Java or a C++ clone. It does things its own way.  In particular, it doesn't do inheritance  [ ... ]



JavaScript Jems - Objects Are Anonymous Singletons

JavaScript should not be judged as if it was a poor version of the other popular languages - it isn't a Java or a C++ clone. It does things its own way.  In particular, every object can be regard [ ... ]


Other Articles

 

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:0596805527>

<ASIN:193398869X>

<ASIN:0137054890>

<ASIN:1449381871>

<ASIN:1430230541>



Last Updated ( Sunday, 10 May 2015 )