Relationship of Function object and Javascript object -


recently read tutorial says if define function below.

function animal() { } 

on surface, code seems create function called animal. javascript, full truth more complicated. happens when code executes 2 objects created. first object, called animal, constructor function itself. second object, called animal.prototype, has property called animal.prototype.constructor, points animal. animal has property points prototype, animal.prototype.

but have little confuse .what function object ?what use animal object?

and if write code below .

var test= new function(); 

and inspected variable test in developer tool of chrome. found test nothing function. can tell me why ? thanks.

enter image description here

updated

the diagram below objects relationship when code executed, please review it. if understanding wrong. please correct me. thanks.

if understanding wrong. please correct me. thanks.

that blog post goes lot of detail that's interesting unnecessarily confusing people of time.

first, let's talk functions; forget prototypes minute. when create function:

function whatever() {   // ... } 

you've created object. is, functions objects, , they're constructed via function built-in constructor. symbol "whatever" in example have value reference object.

given reference function, it's possible call it:

whatever(); // call function 

it's possible take value (the reference function object) , assign variable, or pass parameter function, or use other value in javascript.

var = whatever; another(); // calls "whatever" function 

constructing function via function constructor explicitly that's done, gives function that's otherwise unremarkable. (in op, constructed function doesn't because no code passed function constructor.)

now, things interesting when function invoked part of new expression.

var test = new whatever(); 

by using new, new object instantiated , associated "whatever" function. "whatever" function constructor new object.

every function object, whether it's ever used constructor or not, has associated "prototype" object. when function is used constructor, objects constructs (that is, objects made in new expressions invoke function) implicitly associated prototype object.

the prototype object becomes interesting when object property reference expression evaluated. object property references this:

obj.name obj[ nameexpression ] 

in such expression, property name (either identifier used in . expression or value of expression inside [ ]) checked against set of properties on object directly. if name not same 1 of object's properties, runtime consults prototype object associated constructor function used make object.

for code people write, relationship , of direct implications things worry about. don't have fool around (non-standard, @ time) "proto" property of objects unless you're putting sort of library or framework.

finally, might instructive @ object.create() function , in particular "polyfill" shown in documentation.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -