-3

As i understand, in a object based language, everything that we write/code is an object. but i have a small doubt here. When a simply type "aa". in console, i see so many methods(properties) available to the string from the window object. but if i type a number let's say 10. i don't see ant methods(properties) available for the same. PFA for the images.

Thanks in advance. Screen shot of the above mentioned examples in the chrome console

manjum2050
  • 45
  • 2
  • 6
  • `console.log("aa")` should output simple string `aa`, same for integer. And no, JS is not OOP. But you can still write it in OOP style – Justinas Jan 31 '17 at 10:58
  • "aa" is a string, the methods you see are [**string methods**](http://www.w3schools.com/js/js_string_methods.asp). – Mistalis Jan 31 '17 at 10:59
  • Read up on JavaScript primitive data types. – Brovoker Jan 31 '17 at 10:59
  • 1
    Related: http://stackoverflow.com/questions/4046342/why-dont-number-literals-have-access-to-number-methods – JJJ Jan 31 '17 at 11:01
  • Js is a prototype based programming It also has flavors of object based programming if one can write it adhering to oops concepts of JS – Vinod Louis Jan 31 '17 at 11:02
  • Javascript is object based, but not like others, In js we have not data type. All are objects. But when you type "aa" the console knows what methods you need and suggest them to you. In c# you have to use only suggested methods, But in js you can use the methods that not suggested. – Farzin Kanzi Jan 31 '17 at 13:28

3 Answers3

2

As i understand, in a object based language, everything that we write/code is an object.

No, in an object-based language, objects can exist, but not everything is an object.

In js, there are primitive types (like a number) and objects. You can check this doc from mozilla about the different types is js.

A.Perrot
  • 323
  • 1
  • 8
1

As noted in the comments "aa" is an instance of String and thus the intellisense in Chrome's developer tools will list the instance methods of String.

While 10 is an instance of the Number type, it's behaviour is not quite the same.

Eg. 10.toString() fails with "Uncaught SyntaxError: Invalid or unexpected token".

But (10).toString() does work. But no intellisense.

Summary: there are some parsing oddities in JavaScript (literal are not always instances of the nominally equivalent inbuilt type, and not all tools are 100% consistent in their support in all cases.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • Well, to be more precise `"aa"` is a string _primitive_ not an instance of `String`. Can be easily verified with this `"aa" instanceof String` which returns `false`. However, when you do `"aa".toString()` (or any other method that `String` has) the _primitive_ is implicitly converted to _an object_, the method is executed and then the object is sacrificed to the gods of GC. – VLAZ Jan 31 '17 at 11:11
  • @Vlaz Thanks for your answer. so that means excluding the primitive types, everything is object in JS. is my understanding right? – manjum2050 Jan 31 '17 at 13:21
  • @manjum2050 I suppose you can think of it like that. Yes, primitives are not (but can be dynamically converted as shown). If we have to be even more technical, keywords and operators are also not objects. I believe Smalltalk did do operators as objects. Unless I am misremembering the language but I think at least one had _everything_ as an object. – VLAZ Feb 01 '17 at 13:21
0

It is an object if you try to assign it to any variable. But when you input "." it could be decimal value for number.

Sachin Chauhan
  • 356
  • 1
  • 11