Every Object is a function and every function is Object - Which is Correct?

I was reading this link JavaScript_syntax

This seems to be cyclic - that every function is an Object and every Object itself is a function. Which is the atomic one? Can someone explain in a better way?

Answers:

Answer
  1. Anything that is not a primitive type (undefined, null, number, string, boolean) is an object (or an instance) in JavaScript. That means function inherits from object.

  2. Object instances can contain more instances which can be functions. That's what we call a "method" (since it has an automatic this variable).

  3. Since you can't "call" every Object instance, not every object is a function.

Answer

I think this concept is often misunderstood.

A utility to visualize JS types relationship http://jstype.herokuapp.com/#/home

Javascript Data Types

  1. Primitive types - numbers, strings, booleans, null and undefined.
  2. All non-primitive types are object:

var foo = { }; 
var foo = [1, 2, 3]; 
var foo = function abc() { return "hello world"; }; 
var foo = new Number(30); 
var foo = new String("Hello World"); 
var foo = new Boolean(true); 
var foo = new RegExp(/[foo]+/);

// All 'foo` are object. 

  1. All primitive types have a corresponding Constructor Function wiz. Array, Number, String, Boolean, RegExp. As all functions are objects, they are objects too. So we can call them Constructor Function Objects.

  2. Most of the non-primitive type has prototype property where all inherited stuff lives. Math doesn't have prototype.

  3. All objects inherit from Object.prototype which inherits from null.
    object <- Object.prototype <- null

  4. All native functions inherit from Function.prototype which inherits from Object.prototype.
    function <- Function.prototype <- Object.prototype <- null

  5. Arrays inherit from Array.prototype which inherits from Object.prototype.
    array <- Array.prototype <- Object.prototype <- null

Must read MDN: Inheritance and prototype chain
To get confused Stackoverflow: prototype in JavaScript
Stack Overflow: Function prototype explained

Answer

Also Function is always a property of an object.

This mean that all functions in JavaScript is always bound to an object. If you don't specify an object to bind a function to it's bound to the window object (Also called global functions)

..fredrik

Answer

It would be better to say that in JavaScript everything can be treated as an object, that includes primitive types as well as functions types; what the JavaScript interpreter does is that it automatically promotes your primitives and functions to their object wrapper types when you interact with them.

There is also a Function object, an a number of equivalent Wrappers for the other primitives in JavaScript, that means that you can even call methods on functions instances, like:

myFunction(someArg).call(this)

That being said, not every object is in fact a function.

Answer

As others have said, functions are objects that can be passed around by reference like other js objects. Not all objects are functions, only those that are declared as such.

You will often see methods declared like so:

var myFunc = function(foo, bar) {
    ...
};

This is to reinforce the fact that the mthod is a function object and as such it is a property of the object where it is defined, just like any other variable you might define with var.

This is the foundation of the most important feature in javascript, closure.

Answer

Every function is an Object.

I'm not an javascript expert, but I cannot see how every Object is a function. (I can see how every object could be a function, but that's different)

Answer

Quoting from Working with Objects - MDN Docs
section Using Object Initializers last paragraph:

"In JavaScript 1.1 and earlier, you cannot use object initializers. You can create objects only using their constructor functions or using a function supplied by some other object for that purpose. See Using a Constructor Function."

meant that all objects WERE functions! Specifically, upon evaluation, instances or instantiations of functions.

Literally, ALL objects of that vintage were created syntactically with constructs like:

 "newobj = new constructFunctor(arg1,arg2){this.prop1=arg1 /* etc */}(val1,val2)"

AND in the string that makes the object "newobj" there is the word "constructFunctor", the name of a function. The statement is intentionally quoted to impress the fact that it must be eval()'d to be executed. Prior to execution "newobj" is "equated to" a function because the statement MUST have one and "is" one by virtue of "constructFunctor"'s literal existence to define newobj's value upon execution. The quoting, and not, is very intentional in order to elucidate this abstraction. However, because JavaScript DOES have an eval function, this abstraction is in fact intentionally incorporated into the JavaScript language.

The legacy of this is still fundamental to JavaScript, though some syntactic short cuts have been added as "object initializers" using the shorthand notation like: "no={}". That the paragraph quoted above is still resident in the current documentation IS significant for the very reasons noted.

Furthermore, JavaScript also exemplifies the fundamental paradigms of Functional Programming. This defines everything as a function using the abstractions of Recursive Function Theory and the Lambda Calculus! For instance 0(), 1(), 2(), ... are the constant nonary functions better known as 0,1,2,3, ...

JavaScript is completely consistent with a Functional Programming Style approach rather than the common OOPS (pun intended Object Oriented Programming Style).

Answer

Just for supplementary to Aaron Digulla's answer. In javascript not every object is a function. But Object,Array,String and many other built-in objects are functions that used with new operator to create object. I think this is what confused most people.

Answer

javascript everything is a hashtable. Ryan Dahl said this in one of his talks. thats what json is also; a hashtable definition. thats why u can access an object using array notation or object property notation. the value of the hashtable can be a function as well which is a hashtable. or rather an associative array type Object in the console u get { [native code] } which is a hashtable

Answer

Object is an abstract data given to a class and that class is assigned to an object. Object can have properties and properties can hold values and functions. Or simply for the sake of making it easy to understand you can say that anything that is not primitive data type (number,string, boolean, unll & undefined) can be classified as an object.

Answer

the selected answer by Aaron Digulla's is not 100% correct because it says,

Anything that is not a primitive type (undefined, null, number, string, boolean) is an object.

but a string is an object. That is why you can do things like:

myString="Hello World";

x = myString.length;
newString = myString.toUpperCase();
link = myString.link("http://www.hello-world.com/");

And many other methods can be applied to the string object.

You could also initialize the string like:

myString = new String("Hello, World!");

But because a string is also a datatype it is much easier to initialize it by just applying a value.

Not necessarily an answer to the question ... just a clarification/correction of Aaron Digulla's answer.

Answer

Every function is an object. Objects can contain functions (methods) but an object is not necessary a function.

Answer

The selected answer is wrong. In JavaScript everything is a function, except primitive types. Object itself is a function which is called function Object(). Use for example the following code:

<script>
    alert(Object);
</script>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.