-1

So I am reading up on JS with Programming JS applications and JS the goodparts. Reading through objects and functions.. which are apparently the same. According to Crockford himself (and practically everybody). But then I ask: if they are the same then why if I have an object:

var myObject = {
    value: 0,
    increment: function (inc) {
        this.value += inc;
    }
};

I can add a function like so:

myObject.double = function () {
    this.value += this.value;
}

But when I have a constructor (a function..) :

var myConstructor = function() {
     this.value = 0;
     this.increment = function(inc){
         this.value += inc;
     };
};

I cannot add a function like this:

myConstructor.double = function(){
    this.value += this.value;
};

But only over the prototype like this:

myConstructor.prototype.double = function(){
    this.value += this.value;
};

Now how can anybody say that object and function are the same??

Interestingly, the assignment of the "double" method to the constructor does not give an error, only when you create an object afterwards with new and call the double method on this object does it give: "TypeError can not find".

Also, console.log on the constructor gives:

{ [Function] double: [Function] } 

Notice the missing comma between "[Function]" and "double" by the way.. But this probably shows that really functions are actually objects because we just added the double property (a function) to this object.

But then the question becomes... why can I call new on myConstructor and not on myObject ?? How does JS distinguish between them? If they are both objects which can consists of properties, values and functions..?

EDIT: ok, so I get they are not the same, sorry for my imprecise wording. But one question remains: how does JS know the difference between an object with a function property and a constructor to which properties have been added?

var object = {
    property : "value",
    method: function(){

    }
};

console.log(object) outputs to:

{ property: 'asdf', method: [Function] }

and

var myConstructor = function() {
    this.property = "value";
    this.method = function(inc){

     };
};

myConstructor.property = "value";

console.log(myConstructor) outputs to:

{ [Function] property: 'value' }

So if an object has an anonymous function JS knows its a constructor? Or does it know that over the prototype maybe?

8
  • myConstructor.double = function(){ this.value += this.value; } in this case your double function like a static function you add it to class function not a object and this references to window object in this case Commented Jun 21, 2015 at 13:29
  • 1
    It looks like you're paraphrasing Crockford. I'd like to see an exact quote. Commented Jun 21, 2015 at 13:30
  • 3
    No one said that functions and objects are the same. Functions are objects, so adding properties to them works in the same way. But not all objects are functions, and you cannot use them as constructors. Commented Jun 21, 2015 at 13:33
  • I guess, what he means is that a function is also an object, but not the other way around. This is to the contrary of Java for instance, where you cannot put function definitions in variables. (Well, maybe in Java 8, I'm not sure) Commented Jun 21, 2015 at 13:34
  • 3
    @Lucasvw all programmers are human, does that mean that a programmer is the same as a human? Commented Jun 21, 2015 at 13:46

1 Answer 1

0

How does JS know the difference between an object with a function property and a constructor to which properties have been added?

There are different terminologies:

  • Functions are objects which are Function instances.
  • Callable objects are objects with an internal [[Call]] method.
  • Constructors are objects with an internal [[Construct]] method.

Usually they coincide, so it's usual to call them all functions.

Then it's easy:

  • If you attempt to call an object and it has a [[Call]] method, that method will be called. Otherwise it's not callable, so error.
  • If you attempt to instantiate an object and it has a [[Construct]] method, that method will be called. Otherwise it's not a constructor, so error.

Moreover, there is the [[Class]] internal property, which for functions is usually "Function".

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.