-1

I read the below in a tutorial site and am learning about objects but I didn't understand what they mean.

{Arrays, functions, date, regex, …}, are specialized objects. They have special properties and behaviors than normal objects. But they still are key/value pairs.

I can see that arrays have an index which can be the key and the value of that index makes the pair, but how do functions and dates have key value pairs? I don't really understand how they're objects and not just datatypes like a string or a number?

Can someone explain this in a simple way for me please.

j obe
  • 1,759
  • 4
  • 15
  • 23
  • there are many explanations if you do a little goggling. – Cruiser Mar 14 '17 at 14:43
  • 1
    Anything `a.b` is an object property. You can do that with *almost* everything in Javascript; it's all objects. – deceze Mar 14 '17 at 14:45
  • Have you tried making a date or a function? How about attaching some data to it? It's rather straightforward if you tinker with it. `var f = function(){console.log('hello');}; f.extraProperty = 10;` – Mike Cluck Mar 14 '17 at 14:45
  • check this out: `"foo"[0]` or `"foo".length`. Strings have properties too. What do you think are datatypes? I think the word you were looking for is primitives, not datatypes. `null` and `undefined` are the only two values in JS that have no properties or methods. Everything else inherits from `Object`. – Thomas Mar 14 '17 at 14:52
  • @Cruiser I did try but couldn't find anything specifically along what I was asking. Only long guides about objects in general. – j obe Mar 14 '17 at 16:37
  • @deceze thanks hmm ok, I guess I was trying to think of the concept of key value pairs and how functions and dates fit into that and I don't quite get that angle on it. – j obe Mar 14 '17 at 16:38
  • @MikeC Thanks Mike, I tried that but if I didn't add the extraProperty then a function on it's own isn't a key value pair is it? I think I might have just been confused by the wording in the tutorial. Also is there any real application of adding extra properties like that after a function has been created? – j obe Mar 14 '17 at 16:45
  • @jobe It's definitely still a key-value pair. The key would be `extraProperty` and the value would be `10`. There are plenty of applications. For example, maybe you want a function to be able to reference another object and that object should be configurable. You could make that a property of the function. The big thing is that most things are objects and sometimes that comes in handy. Usually, you don't add properties to functions but it's available. – Mike Cluck Mar 14 '17 at 16:47
  • @Thomas thanks but if strings have properties then why are they considered primitives and not data types then. Also if a function can have a property added to it as in the example before by Mike C, then does that make the string an object too....this can get a bit confusing. – j obe Mar 14 '17 at 16:47
  • @MikeC Thanks, what I meant is if you didn't add the extra property then the function on it's own wouldn't be a key value pair would it? – j obe Mar 14 '17 at 16:49
  • 1
    @jobe It still would be. Functions have built-in methods and properties such as [`call`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) and [`apply`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply). If you can reference something by a key and get a value then you have a key value pair. Objects are simply key-value pairs (which may contain more key-value pairs). Functions are merely objects which has a function body which can be called. – Mike Cluck Mar 14 '17 at 16:50
  • 1
    @jobe Even if it didn't have any properties/keys on it, it would still be an object. `var a = {}`. In this example, `a` has no keys and no values but it's still an object. – Mike Cluck Mar 14 '17 at 16:52

1 Answers1

0

Well, both dates and functions have some predefined values, you can also use predefined methods to get them. That's about the reasoning I know.

ahmedhara
  • 7
  • 6