Pierre Giraud
Pierre Giraud

Reputation: 155

JavaScript object meaning

I'm currently doing a bit of JavaScript and I have some troubles trying to explain myself one thing.

If I do that :

var text = new String("Enter any value here"); 

We all agree that "text" is an object, right?

But what should be "String" called then? Because I see everywhere that String is also an object, but wouldn't it be simpler to call it a class like in PhP.

Is there an other name, more precise than just object for String?

If someone can explain me the subtlety, I would be very happy. Thank you!

Upvotes: 0

Views: 125

Answers (4)

laruiss
laruiss

Reputation: 3816

By design, JavaScript (the implementation of the ECMAScript standard) has no class. So String cannot ever be called a class.

String here is but an object, just as almost everything in JavaScript is (as some the answers have already stated).

console.log(String instanceof Object); // true

Actually a special object: a function.

console.log(typeof String); // "function"
console.log(String instanceof Function); // true

And even a special function: a function that should be called as a constructor, hence the capital 'S'.

Even more special function: it is a built-in function (it is "present in the language" and therefore provided by the host environment -the browser or node, for example).

This special function allows you to instantiate an object of type String:

var aString = new String("A string");

But this is the wrong way to do it: you should write this:

var aPrimitiveString = "A string";

which interestingly, by the way, doesn't make text a String (Object), but a string (primitive).

console.log(typeof(aString)); // "object"
console.log(aString instanceof String); // true
console.log(aString instanceof Object); // true

console.log(typeof(aPrimitiveString)); // "string"
console.log(aPrimitiveString instanceof String); // false
console.log(aPrimitiveString instanceof Object); // false

Even in ECMAScript 6 (AKA ES6, AKA Harmony, AKA ES2016), there won't be any classes in the acception you have of it, "classes" in ES6 will still be an object, of type function, with prototypal inheritance.

One more thing: String can also be used to explicitly coerce a value into a string primitive:

var number = 1;

var numberAsString = String(1);

console.log(number); // 1
console.log(numberAsString); // "1"
console.log(typeof number); // "number"
console.log(typeof numberAsString); "string"

for the sake of completeness, I hope you guessed that:

  • number is a primitive number
  • numberAsString is a primitive string
  • new number(1) would be an object, instance of Number and Object
  • Number('0') would be a primitive number
  • same goes for Boolean

There are no classes in JavaScript, there won't ever be (I don't want any of it, anyway), that is what make the language so flexible, expressive and powerful.

Maybe you would want to take a look at this: http://javascript.crockford.com/inheritance.html (and the whole site and work of Douglas Crockford).

Upvotes: 1

Anurag Peshne
Anurag Peshne

Reputation: 1547

I think the source of your confusion is unfamiliarity with Prototypal Inheritance model. I suppose you are well versed with 'Classical Model' where classes are constructed using Classes as blueprint. On the other hand in JavaScript other Objects are created using an Object as a prototype or using an empty object and then pinning new properties in it.

If you try logging typeof(String) you will get output as function. Here when we do a new String('hello, world'), we get a new String object Hello, World. Thus String is a constructor in JavaScript.

Upvotes: 2

kockburn
kockburn

Reputation: 17616

According to w3schools:

In JavaScript, almost "everything" is an object.

  1. Booleans can be objects (or primitive data treated as objects)
  2. Numbers can be objects (or primitive data treated as objects)
  3. Strings can be objects (or primitive data treated as objects)
  4. Dates are always objects
  5. Maths are always objects
  6. Regular expressions are always objects
  7. Arrays are always objects
  8. Functions are always objects
  9. Objects are objects

In JavaScript, all values, except primitive values, are objects.

Primitive values are: strings ("John Doe"), numbers (3.14), true, false, null, and undefined.


MDN says in javascript everything are objects.

In JavaScript, almost everything is an object. All primitive types except null and undefined are treated as objects. They can be assigned properties (assigned properties of some types are not persistent), and they have all characteristics of objects.

Creating new objects

JavaScript has a number of predefined objects. In addition, you can create your own objects. You can create an object using an object initializer. Alternatively, you can first create a constructor function and then instantiate an object using that function and the new operator.


So in javascript the idea of class doesn't really exist like in other languages. It's just an instantiated object or function if you prefer.

typeof 1.00 // number
typeof 1 //number
typeof "string" //string
typeof String("string") //string
typeof new String("string") //object
typeof {} //object
typeof [] //object

Demo


It seems possible to create classes in ECMAScript 6

It is said that:

Defining classes

Classes are in fact functions, and just like you can define function expressions and function declarations, the class syntax has the two opponents:

class expressions and class declarations.

Upvotes: 1

Ely
Ely

Reputation: 11152

JavaScript is an object oriented language without classes.

However, you can apply many object oriented paradigms in JavaScript. It has its own mechanisms to do so.

In JavaScript, instead of classes you use objects. That's really about it already. You can think of String as a class if you want, but sometimes it can lead to confusion, because it is easy to confuse concepts known from Java or C++ with JavaScript. The implementation is just different.

I suggest you omit to think of it as a class, and call it an object. And try not to apply terminology of other languages like PHP with JavaScript.


Now, the confusion might be that in other OO languages you define a class and then you create instances from that class, which we refer to as objects.

In JavaScript you simply create a new copy of another object.

Please refer to this link and...

...note that JavaScript distinguishes between String objects and primitive string values. (The same is true of Boolean and Numbers.)

Upvotes: 1

Related Questions