Everything is an object

why everybody says “everything is an object” in javascript. can’t understand this sentence, hope someone can explain it to me. the more detail,the better.

Sorry, if we were speaking Dutch it would have made more sense because apostrophes are used for plurals sometimes : ) “Foos” meant “more things called Foo”, or, just a plural of “Foo”. The first object was called Foo. If it was called “Car” then the others would have been “Cars” (meaning, many Car objects).

So Fu is an object who inherits from Foo. Speaking casually, I could call it a type of Foo. If I have a bunch of objects inheriting from Foo, I might refer to them as “Foos” to a colleague.

stomme
in your answer,you said i can make new Foos and they can all have the same stuff you gave your original Foo.

why in this code,

var Fu = new Foo();
there is no Foos ?

Well, in Javascript just about everything IS an object… object here meaning “a glob of stuff” (pretty broad, I know) which follows certain rules. For now, keep this separate from object-oriented programming… that is somewhat different.

So with a “glob of stuff that follows certain rules”, a real-world example might be the Animals example (yeah also used in OO programming I know). If you define something as an Animal, you know you’re not going to be thinking of Rocks or Plants or whatever… because certain properties are inherently “Animal-ish” (they are alive… they can’t make food from the sun… whatever).

So the primitive types in Javascript (Strings, Numbers, Booleans…) are objects in that they have their own methods and properties. Every time you say something like
var foo = “bar”;
you’ve made something that ultimately is a type of object (what kind of object? a String, so you could later come around and perform String.methods on foo if you want, like counting chars and splitting them etc).

A function is an object. An array is an object. A primitive is an object. And then, there are the ones you make yourself.

var Foo = {};

Foo.bar = something;

Foo.baz = something;

You can add whatever you want to this newly minted Foo object. You can make new Foos and they can all have the same stuff you gave your original Foo.

var Fu = new Foo();

(now you don’t have to say
Fu.bar = something; or
Fu.baz = something;
cause unless you say otherwise, they are already there)

Javascript is a little special in that the way baby Foos can have the same methods and properties of the first Foo is via Prototyping… the way Javascript passes information from one object to another object (inheritance… known here as “prototypical inheritance”). Most other languages use Classes for this (classical inheritance)… very similar but not exactly the same.

Basically, object-oriented programming is using these Objects (whether you create them or they are any of the native ones pre-built into the language) to interact with each other, share methods, do jobs together and separately…
as opposed to having some long list of instructions that Do Stuff (imperative programming… imperative means “to command”).

Remember with Javascript you can also do Functional programming, which it’s also very suited to because almost everything is a function (and the functions are objects) and functions are first-class (they’re already built into the language and you can pretty much do what you want with them). So, you can have closures, recursion, and if you want you can go ahead and calculate stuff and just store the results somewhere (they don’t have to Do Anything) or give those results to another function as an argument.

Now I likely have some things not entirely correct so a guru should come in here to elaborate : )

Good question.

Aswel, what then makes the difference between OO programming and regular structured programming?