1

Can anyone explain this ?

I read that functions are just an object in Javascript just that is callable . I.e a function is a subset of object (hashmaps) .

However an object is created using function like this

function Constructor() {}; a = new Constructor();

a = {} ; //a.constructor is the 'Object' Function 

//And Douglas Crockfords Object.create(a) does this

Object.create = function(obj){
function F();
F.prototype = obj;
return new F();
}

So the question is that if an object itself is created from a function , how can it be the superset ? I am sure my logical reason is failing somewhere but not quite clear as to what is it that I fail to understand ! Feels like a bit of Chicken and Egg problem . Can anyone help with my logical fallacy ?

PS: This question's essence has something to do with this What is a metaclass in Python?

Community
  • 1
  • 1
Nishant
  • 20,354
  • 18
  • 69
  • 101
  • 1
    Looks like you have seriously misunderstood what you have read. What it means is simple - functions are also objects. You can assign functions to variables and pass them as function parameters like you do with any other objects. That's all it means. No surprise, no logical fallacy, no chicken, no eggs! – RaviH Jun 12 '14 at 07:48
  • But the {} and new are a bit different from these normal concepts . Functions being first class citizens is right . – Nishant Jun 12 '14 at 08:39

3 Answers3

2

Constructor functions do not create objects. The exact action "create an object" doesn't exist as a construct in javascript - although there are constructs that involve creating an object(and doing other things alongside it) - object literals {}, the new operator, etc.

See for example the new operator. When you use it with a function, it calls the function's internal [[Construct]] property : http://es5.github.io/#x13.2.2 :

1. Let obj be a newly created native ECMAScript object. - you could say it's created by the system or some other way to describe an external source.

After the object is created, it is passed to the constructor function as the this parameter:

8. Let result be the result of calling the [[Call]] internal property of F, providing obj as the this value and providing the argument list passed into [[Construct]] as args.

Then the constructor function can decorate it, add, modify properties, and so on. But it's not the function itself creating the object.

Sacho
  • 2,169
  • 1
  • 14
  • 13
  • Right as someone told me now - unless you do "return {};" or something, the function doesn't create the object, the "new" keyword does. The subtle difference between what new returns and what function returns . – Nishant Jun 12 '14 at 08:07
  • Even when you do `return {}`, it's still not the function creating the object - an object literal `{}` initiates an algorithm which involves calling `new Object()`, which, as I described, creates an object *from thin air*, so to say, and then passes it along. – Sacho Jun 12 '14 at 08:09
1

What's an object? It's a software component that encapsulates state and behavior together into a single entity in memory.

By that definition, you can see where everything can be thought of as an object. Functional programmers make functions first class objects. Data folks would say that data, even that without behavior, can be thought of as an object (albeit not a very smart one).

JavaScript treats functions as objects.

You are confusing the Object that is exclusive to JavaScript with the "object" of OOP.

gauravmuk
  • 1,606
  • 14
  • 20
0

Answering my own question :

I read that functions are just an object in Javascript just that is callable . I.e a function is a subset of object (hashmaps) .

functions (javascript functions) are subset of hashmaps or objects (but not that is not javascript objects) it is the general concept of objects as mentioned by jonLuci .

What's an object? It's a software component that encapsulates state and behavior together into a single entity in memory. functions in javascript is a part of this software conception of objects/hashmaps .

So the question is that if an object itself is created from a function , how can it be the superset ?

The logical fallacy in this is that, object in this context should read as "javascript object" and not the object in the generic sense of the word . javascript objects are created from functions as explained by the chosen answer!

In other words

hashmaps(objects in generic sense) > functions > javascript objects

> is used to mean kind of a superset relation .

javascript objects having callability i.e () can be considered to be javascript functions . javascript functions are just hashmaps or objects in the very generic sense of the term !

Nishant
  • 20,354
  • 18
  • 69
  • 101