I'm running this javasrcipt code on google chrome browser as an html script tag.
<script type="text/javascript">
var bar=function() {
var name='tanzeel';
console.log('inside a function');
}
console.log('My name is ' +bar.name);
</script>
I'm coming from java and cpp background. My knowledge about oop says that function in js are instantiated as an object and here variable bar is pointing to that object. So I can access all the properties of that object. Then why console.log('My name is ' +bar.name) is printing My name is bar instead of the string My name is tanzeel.
Please correct me where i am wrong and what else do i need to know. Thanks.

bar.nameis accessing the name of the function. If you want a constructor, you need to usenew bar()as well as assigningthis.name, sincevar nameis a function-scoped variable.Function.prototype.nameclasskeyword very similar to java classes. And try to avoidvarinstead useletorconstnamelikelength(the number of arguments defined)call()andbind(), ... And that you can store functions/methods in variables and pass them around like any other value.