What's substantive difference between function and Object in javascript? -
when learn js @ first voice object, think maybe function object , object object too.
but when learn prototype, thing different thought.
function helloworld(){ this.hi = 'world'; } var foo = { 'sth':'happend' }; function bar(){}; bar.prototype = foo; console.log(new bar().sth); bar.prototype = helloworld; console.log(new bar().hi);
and print
happend undefined
then replace bar.prototype = helloworld;
bar.prototype = new helloworld();
correct result.
happend world
i'm newbie, maybe it's stupid question, want know what's wrong in mind? function not object? me? lot..
yes, function object, , can have properties well:
var foo = {}; var bar = function(x){return x+1}; foo.prop = "hello "; bar.prop = "world!"; // works same! alert(foo.prop + bar.prop);
what's substantive difference between function , object?
well, function object can called - "normal" objects can't:
bar(4); // 5 foo(4); // error: 'foo' not function
i replace
bar.prototype = helloworld;
bar.prototype = new helloworld();
correct resulti want know what's wrong in mind?
you must not confuse constructor functions instances created calling them. helloworld
object (just bar
in example above), it's different object new helloworld()
(which inherits helloworld.prototype
, , initialised constructor hi
property).
Comments
Post a Comment