Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is almost everything in Javascript an object?

Tags:

javascript

oop

I read this a lot in many JavaScript introductions. I just don't understand it. I always think of objects as something with methods and properties. Arrays I understand, since it has key value pair. How about "Strings" or "Numbers" or "functions" ? These things above listed seem to be like functions to me. This means you input something, you get something out. You don't really get the access properties or anything. There's no dot notation used in arrays or this list of "objects".

Does anyone code some examples of each of these with dot notations which its methods and properties are being accessed? I suspect that definition of object is probably limited since I did start learning about JavaScript...

like image 316
Matt Avatar asked Feb 02 '12 08:02

Matt


People also ask

How is everything in JavaScript an object?

In JavaScript, everything is an object, even when it's something else. Functions are objects. Strings are objects. Numbers are objects.

Why is everything a function in JavaScript?

Everything in JavaScript happens in functions. A function is a block of code, self contained, that can be defined once and run any times you want. A function can optionally accept parameters, and returns one value. Functions in JavaScript are objects, a special kind of objects: function objects.

Why JavaScript array is object?

The Array object lets you store multiple values in a single variable. It stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

What is true about JavaScript objects?

JavaScript Object Methods This method returns an array with arrays of the key, value pairs. This method prevents existing properties from being removed. This method returns a property descriptor for the specified property of the specified object. This method returns all own property descriptors of a given object.


1 Answers

No, not everything is an object in JavaScript. Many things that you interact with regularly (strings, numbers, booleans) are primitives, not objects. Unlike objects, primitive values are immutable. The situation is complicated by the fact that these primitives do have object wrappers (String, Number and Boolean); these objects have methods and properties while the primitives do not, but the primitives appear to have methods because JavaScript silently creates a wrapper object when code attempts to access any property of a primitive.

For example, consider the following code:

var s = "foo"; var sub = s.substring(1, 2); // sub is now the string "o" 

Behind the scenes, s.substring(1, 2) behaves as if it is performing the following (approximate) steps:

  1. Create a wrapper String object from s, equivalent to using new String(s)
  2. Call the substring() method with the appropriate parameters on the String object returned by step 1
  3. Dispose of the String object
  4. Return the string (primitive) from step 2.

A consequence of this is that while it looks as though you can assign properties to primitives, it is pointless because you cannot retrieve them:

var s = "foo"; s.bar = "cheese"; alert(s.bar); // undefined 

This happens because the property is effectively defined on a String object that is immediately discarded.

Numbers and Booleans also behave this way. Functions, however, are fully-fledged objects, and inherit from Object (actually Object.prototype, but that's another topic). Functions therefore can do anything objects can, including having properties:

function foo() {} foo.bar = "tea"; alert(foo.bar); // tea 
like image 161
Tim Down Avatar answered Sep 22 '22 22:09

Tim Down