Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy prototype is undefined
    primarykey
    data
    text
    <p>I known this has been asked hundreds of times, however, I can't seem to grasp the concept of <code>prototype</code></p> <p>Here's my sample script</p> <pre><code>var config = { writable: true, enumerable: true, configurable: true }; var defineProperty = function(obj, name, value) { config.value = value; Object.defineProperty(obj, name, config); } var man= Object.create(null); defineProperty(man, 'sex', "male"); var person = Object.create(man); person.greet = function (person) { return this.name + ': Why, hello there, ' + person + '.' } var p=Object.getPrototypeOf(person); alert(p.sex);//shows male person.prototype.age=13;//why there is a error said the prototype is undefined? I thought it supposed be man object... var child=function(){} child.prototype.color="red";//why this line doesn't show error? both child and person are an object . alert(child.prototype.color);//shows red var ch=Object.getPrototypeOf(child); alert(ch.color);//why it is undefined? it is supposed red. </code></pre> <p>Hope you can give me some helps... thanks.</p> <p><strong>Updated:</strong></p> <p>Thanks your guys kindly help, Based on Elclanrs's answer, Below is what I learned.</p> <p><code>Function</code> is the one of the build-in objects in javascript. the 3 format creation function object are equal.</p> <pre><code>var function_name = new Function(arg1, arg2, ..., argN, function_body) function function_name(arg1, arg2, ..., argN) { ... } var function_name=function(arg1, arg2, ..., argN) { ... } </code></pre> <p>So, that is why create a prototype chain we have to create a function and then call it with the new keyword .</p> <p><code>Function.prototype</code> is the reference to All the Function object <code>prototype</code>.</p> <p>Cheers</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload