Reputation: 33
I am learning object oriented javascript, from Object Palyground
according to the tutorial, function itself is considered as an object and i know that you can get the properties of an object using in keyword in javascript
var obj = {a:"hello",b=123};
for(var prop in obj)
alert(var);
The above code gives me the keys present in the function object obj as expected
but when I use the same code for this situation
var funObj = function(a,b){
alert("hello");
}
for(var prop in funObj)
alert(prop);
or in this case even
function myFunction(a,b){
alert("hello");
}
for(var prop in myFunction)
alert(prop);
It does not give me any output, Ideally according to the tutorial the function object should comprise of three properties that are: name, length and prototype but i am getting none, where i am doing wrong
kindly help me
Upvotes: 1
Views: 91
Reputation: 10899
A Function
in JavaScript can be referred to as "first class" as it is a callable object meaning it is an object with callable semantics added to it. In some ways an Array
in JavaScript is similar to a Function
in that they are both objects but have special features included. NOTE: A common statement uttered about JavaScript is that "everything is an object" but this is not true however. None of the simple primitives (string
, boolean
, number
, null
, and undefined
) are objects (although JS does some nifty tricks if you attempt to treat them as such).
var obj = {bar: 'hi'};
Object.keys(obj); // ["bar"]
function func() {}
Object.keys(func); // []
func.bar = 'hello';
Object.keys(func); // ["bar"];
var arr = [1, 2, 3];
Object.keys(arr); // ["0", "1", "2"]
arr.bar = 'hey';
Object.keys(arr); // ["0", "1", "2", "bar"]
bar
shows up in all three because those properties are set to be enumerable by default. However as you noted a function has some other properties but those aren't showing up. Why? Let's look at obj
first.
obj.propertyIsEnumerable('bar'); // true
bar
by default was set to be enumerable
.
Now lets look at bar
in func
.
func.propertyIsEnumerable('bar'); // true
So that explains why bar
shows up for func
but what about name
?
func.propertyIsEnumerable('name'); // false
Ahah! It is not enumerable. This is true for a lot of properties/methods that are automatically linked to or assigned to an object via the JavaScript engine. For instance, the length
property of an array object.
arr.propertyIsEnumerable('length'); // false
That all said, if you are new to JS I would highly recommend reading the You Don't Know JS series by Kyle Simpson. It's meant for people with some programming experience but they are very useful for learning what JS is and what it's not (no matter how much syntactic sugar gets piled on). I would especially recommend focusing on the key point of the difference between classical inheritance and JavaScript's OLOO (Objects Linking to Other Objects).
The intro book Up & Going is a quick read that covers the basics of what the entire series will dive deeper into. The rest of the series (so far) includes: Types & Grammar, Scopes & Closures, this & Object Prototypes, Async & Performance, and ES6 & Beyond. You can also preview them all on his github repository.
OR You can also review MDN's documentation for Working with Objects for some very good information.
Upvotes: 0
Reputation: 1905
var obj = {a:"hello",b=123};
if not the same as
var obj = function(a, b) {}
If you want A and B to be properties, you should do it that way:
var obj = function(a, b) {
this.a = a;
this.b = b;
}
That way, obj.a and obj.b make a little more sense, but not that much yet. function(){} is you constructor. So you need to actually build an instance in order to use it the right way.
var myObj = new obj('value for a', 'value for b');
for (var prop in myObj) {
// Prop = a || b
console.log(prop);
// Values for a and b
console.log( myObj[ prop ] );
}
If you use it in a static way, you do NOT have to do the function thing. Also not that object don't have the length method. Arrays do.
To make this REALLY SIMPLE, and totally not accurate, {} (objects) are kinda static and function(){} are instanciable (does that word even exists?), which means you can use the "new" and therefore benefit from the function constructor.
This is all really technic, and it looks like you're beginning with javascript. Tell me if I was unclear or you need some precision.
Hope that helps.
Upvotes: 0
Reputation: 816482
where i am doing wrong
for...in
only iterates over enumerable properties. The default properties of native objects are often non-enumerable.
You could use Object.getOwnPropertyNames
to get a list of properties of the function object:
> Object.getOwnPropertyNames(function() {})
["length", "name", "arguments", "caller", "prototype"]
Or if you just want to inspect the properties for learning purposes, use console.dir
to log the function:
Upvotes: 4