1
 let person ={
        name:"ayush",
        class:12
    }
function man(){
        
    }

In case of function man if I write man.proto it gives ƒ () { [native code] }

when I write man.prototype I am getting {constructor: ƒ}

but in case of object person if i wirte person.proto it gives me {constructor: ƒ, defineGetter: ƒ, defineSetter: ƒ, hasOwnProperty: ƒ, lookupGetter: ƒ, …}

when I write person.prototype it gives undefined

Why results varries in case of object and function?

  • 1
    Easy, just never use `__proto__` – VLAZ May 05 '21 at 16:58
  • 1
    You might be confused about the fundamentals. The fundamental rule is *the prototype of object `new f()` is `f.prototype`.* Does that make sense to you? If yes, then you can figure out the answers to your own questions; drawing a diagram where boxes are objects and labeled arrows are properties of the object will help. If no, then work on understanding the fundamentals. – Eric Lippert May 05 '21 at 16:58
  • Once you get that fundamental rule in your head then your observations start making sense. Why is `person.prototype` undefined? Because there is no such thing as `new person()`. If `person` were a function then it would need a `.prototype` property so that `new person()`'s prototype is `person.prototype`. But `person` is not a function. Does that make sense to you? If not, can you ask a more focused question that would unblock you? – Eric Lippert May 05 '21 at 17:03
  • The other thing you need to know to understand what is going on is: `person` behaves as though it was a `new Object()`, so its prototype is `Object.prototype`. `man` behaves as though it was a `new Function()` so its prototype is `Function.prototype`. That all follows from the fundamental rule. – Eric Lippert May 05 '21 at 17:06
  • Can you tell me any source from which I can learn prototype basics? because I am new to programming and today only I started prototype concept after completing basics of java script –  May 05 '21 at 17:29

1 Answers1

0

You should never use the .__proto__ property, it still exists for backwards compatibility but is considered deprecated in favor of Object.getPrototypeOf().

Now to explain what you observed:

let person = {
    name: "ayush",
    class: 12
}

function man() {}

console.log(Object.getPrototypeOf(person));
// Object.prototype because person is an instance of Object

console.log(person.prototype);
// undefined because an object has no prototype property, it's not a constructor function

console.log(Object.getPrototypeOf(man));
// Function.prototype

console.log(man.prototype);
// object. man.prototype is actually an empty object to be filled with
// methods for future use as a constructor function with the new operator
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Guerric P
  • 30,447
  • 6
  • 48
  • 86