These are chat archives for FreeCodeCamp/HelpJavaScript

May 2018
May 26 2018 06:33

Hey guys, I've been learning some OOP and I'm getting confused with whats the relationship between Object and Function inbuilt constructors.

Object instanceof Function // true
Function instanceof Object //true

How is this possible?
Also, I have doubt regarding how Object or any built-in Objects are able to reference its own prototype.

function Person() {
Person.prototype.greet = function() {console.log('hello');}
Person.greet // undefined

Object.prototype.farewell = function() {console.log('goodbye');}
Object.farewell // f () {console.log('goodbye');}  => How is it able to reference its own prototype?

Also, Object.getPrototypeOf(Function) === Object.getPrototypeOf(Object) //true
How is this possible again?

May 26 2018 06:49

@rajataudichya That doesnt explain how Object.constructor returns Function constructor. From my understanding and MDN

All objects in JavaScript are descended from Object; all objects inherit methods and properties from Object.prototype, although they may be overridden.

So it doesnt make sense how Function is behind an Object, shouldnt it be other way around?

Angela Sirls
May 26 2018 07:02
Can someone please help me with figuring out why this challenge doesn't work?
Can someone please help me to determine why I can't get the Multiple Identical Options in Switch Statements to run?
abraham anak agung
May 26 2018 07:08
@annDAcoder_gitlab i think 456 should return Mid and you don't need () for Low
May 26 2018 07:14
@annDAcoder_gitlab The quotes around the string looks unicodes one - replace them with normal single or double quotes. Also, as mentioned above the 2nd condition should be "Mid".
May 26 2018 07:24
hey guys, does anyone use bitbucket, cant push my local files
May 26 2018 07:30
nevermind got it
Pieter Stokkink
May 26 2018 09:54

@ezioda004 Great question earlier about Function/Object. I don't know a complete answer, I just know that Object is a callable, you can do Object(), so it is indeed a function, and since everything in JS (except primitives) is an Object vice versa would also hold. Playing around in my console, I found that I could use Function methods directly on Object:

Object.apply({})  //-> {}
Object.bind({}) // -> f Object() { [native code]}

But the reverse often gives errors:

Function.values() // TypeError
Function.defineProperty(window, 'ha', {value: 'ha'}) // TypeError

I wonder how deep this rabbithole goes.

May 26 2018 10:00
@forkerino hi, do you use bitbucket?
May 26 2018 16:41
@forkerino So does that mean Object comes before Function in prototype delegation chain?
Also I found something interesting playing around with __proto__
Both Function and Object have same prototype chain
Function.__proto__ === Object.__proto__ //true
Function.__proto__.__proto__ === Object.__proto__.__proto__ //true
Function.__proto__.__proto__.__proto__ === Object.__proto__.__proto__ .__proto__ //true

console.dir(Function.__proto__.constructor); // Function() (.prototype) same as Object.__proto__.constructor
console.dir(Function.__proto__.__proto__.constructor); // Object() (.prototype)  same as Object.__proto__.__proto__.constructor
console.dir(Function.__proto__.__proto__.__porto__); // null which is at the bottom of the chain and has no prototype. Same for Object.__proto__.__proto__.__proto__
May 26 2018 18:57
@IsaakNazar Curious, why bitbucket over gitlab or github?
Nate Mallison
May 26 2018 19:03
@ezioda004 this is from YDKJS: "function is a sub-type of object (technically, a "callable object"). Functions in JS are said to be "first class" in that they are basically just normal objects (with callable behavior semantics bolted on), and so they can be handled like any other plain object."
Functions are objects and objects are functions as you have seen. Functions are just special objects that allow you to tack on a name and call them. Behind the scenes I don't think it's any different than you doing
Const obj = { }
Well I can't type it all out. On mobile. But making an object with methods is no different. Remember JS is not an OOP language
Pieter Stokkink
May 26 2018 19:10
@IsaakNazar Sorry missed your message, I haven't used bitbucket.
May 26 2018 19:12

@NJM8 Yes, I'm aware of that. I was just curious whats the relationship b/w Object and Function in -built objects.

Object instanceof Function // true
Function instanceof Object //true


Object.prototype.farewell = function() {console.log('goodbye');}
Object.farewell // f () {console.log('goodbye');}

How Object is able to reference its own prototype which is still confusing to me.
Its arguable that JS is not a OOP language. Its not classical and pure OOP but its follows prototypical inheritance so yeah its OOP.

Pieter Stokkink
May 26 2018 19:25
@ezioda004 Furthermore, you can after your last lines of code also do Function.farewell
Joyce Tsan
May 26 2018 20:07
hi, I ran into a js problem. can someone take a look at my code?
May 26 2018 20:38
don't ask to ask just ask
Norvin Burrus
May 26 2018 23:21
@gulsvi this may be useful: :sparkles: