/* Input: */
function doSomething(){};
console.log( doSomething );
/* Output: */
function doSomething(){}
/* Input: */ /* Input: */
function doSomething(){} function doSomething(){}
var doSomeInstancing = new doSomething();
console.log( doSomeInstancing ); console.log( doSomething.prototype );
/* Output: */ /* Output: */
doSomething {}
{
__proto__: Object Object {constructor: function}
{ {
constructor: function doSomething() constructor: function doSomething()
__proto__: Object __proto__: Object
{ {
constructor: function Object() constructor: function Object()
hasOwnProperty: function hasOwnProperty() hasOwnProperty: function hasOwnProperty()
isPrototypeOf: function isPrototypeOf() isPrototypeOf: function isPrototypeOf()
propertyIsEnumerable: function propertyIsEnumerable() propertyIsEnumerable: function propertyIsEnumerable()
toLocaleString: function toLocaleString() toLocaleString: function toLocaleString()
toString: function toString() toString: function toString()
valueOf: function valueOf() valueOf: function valueOf()
} }
} }
}
/* Input: */ /* Input: */
function doSomething(){} function doSomething(){}
doSomething.prototype.foo = "bar"; doSomething.prototype.foo = "bar";
console.log( doSomeInstancing ); console.log( doSomething.prototype );
/* Output: */ /* Output: */
doSomething {}
{
__proto__: Object Object {foo: "bar", constructor: function}
{ {
foo: "bar" foo: "bar"
constructor: function doSomething() constructor: function doSomething()
__proto__: Object __proto__: Object
{ {
constructor: function Object() constructor: function Object()
hasOwnProperty: function hasOwnProperty() hasOwnProperty: function hasOwnProperty()
isPrototypeOf: function isPrototypeOf() isPrototypeOf: function isPrototypeOf()
propertyIsEnumerable: function propertyIsEnumerable() propertyIsEnumerable: function propertyIsEnumerable()
toLocaleString: function toLocaleString() toLocaleString: function toLocaleString()
toString: function toString() toString: function toString()
valueOf: function valueOf() valueOf: function valueOf()
} }
} }
}
/* Input: */ /* Input: */
function doSomething(){} function doSomething(){}
doSomething.prototype.foo = "bar"; doSomething.prototype.foo = "bar";
doSomeInstancing.prop = "some value"; doSomeInstancing.prop = "some value";
console.log( doSomeInstancing ); console.log( doSomething.prototype );
/* Output: */ /* Output: */
doSomething {prop: "some value"}
{
prop: "some value"
__proto__: Object Object {foo: "bar", constructor: function}
{ {
foo: "bar" foo: "bar"
constructor: function doSomething() constructor: function doSomething()
__proto__: Object __proto__: Object
{ {
constructor: function Object() constructor: function Object()
hasOwnProperty: function hasOwnProperty() hasOwnProperty: function hasOwnProperty()
isPrototypeOf: function isPrototypeOf() isPrototypeOf: function isPrototypeOf()
propertyIsEnumerable: function propertyIsEnumerable() propertyIsEnumerable: function propertyIsEnumerable()
toLocaleString: function toLocaleString() toLocaleString: function toLocaleString()
toString: function toString() toString: function toString()
valueOf: function valueOf() valueOf: function valueOf()
} }
} }
}
function doSomething(){}
var doSomeInstancing = new doSomething();
doSomething.prototype.foo = "bar";
doSomeInstancing.prop = "some value";
console.log( doSomeInstancing.prop ); // some value
console.log( doSomeInstancing.foo ); // bar
console.log( doSomething.prop ); // undefined
console.log( doSomething.foo ); // undefined
console.log( doSomething.prototype.prop ); // undefined
console.log( doSomething.prototype.foo ); // bar
console.log( doSomething ); // function doSomething(){}
参考文章:对象原型