You're adding bar to the Object.prototype:
console.log(fooProto.constructor===Object);//true
console.log(object.constructor.prototype===fooProto);//true
After setting the prototype of f()
you should repair the constructor:
f.prototype=...;
f.prototype.constructor=f;
If you were expecting undefined, undefined then I've just answered why the second one doesn't print undefined.
Why the first one isn't undefined is answered here in the introduction to constructor functions. Short answer; when you request members on an object and that object doesn't have that member/property then JS will look for it on the prototype of the constructor that created the object.
For example:
var test = {};
console.log(test.hasOwnProperty);//=hasOwnProperty(), not undefined
Test doesn't have a hasOwnProperty so where did it come from?
console.log(test.constructor);//=Object()
console.log(test.constructor.prototype.hasOwnProperty
===test.hasOwnProperty);//=true
So test got hasOwnProperty from Object.prototype and Object is the constructor of test