Every Object is a function and every function is Object
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?
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
.
Object instances can contain more instances which can be functions. That's what we call a "method" (since it has an automatic this
variable).
Since you can't "call" every Object instance, not every object is a function.
Every function is an object. Objects can contain functions (methods) but an object is not necessary a function.
I think this concept is often misunderstood.
A utility to visualize JS types relationship http://jstype.herokuapp.com/#/home
Javascript Data Types
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.
链接地址: http://www.djcxy.com/p/76346.html
上一篇: JavaScript:if(!x)`和if(x == null)`有什么区别?
下一篇: 每个对象都是一个函数,每个函数都是对象