Functions as objects

R

relaxedrob

Howdy All!

I am still getting my head around a few base concepts. Any comments or
criticisms on the below definitions would be most welcome!

A function is an object.

JavaScript objects have properties:
- a var (which is an object or a primitive);
- a method (which is a function assigned to the object).

Note that since methods are functions and functions are objects we
could re-write the above to reflect a different view of an object's
properties.

JavaScript objects have properties:
- other objects (declared as a var or method);
- primitive vars.

Thanks for any advice!

Rob
:)
 
D

Douglas Crockford

A function is an object.
JavaScript objects have properties:
- a var (which is an object or a primitive);
- a method (which is a function assigned to the object).

Not exactly. Vars are variables that are bound to a function. These are distinct
(or at least should be) from the members of an object. A member whose value is a
function can be considered a method, although the language itself does not make
this distinction.

Common members of functions include the .prototype object and the .apply method.

http://www.crockford.com/javascript/inheritance.html
 
L

Lasse Reichstein Nielsen

I am still getting my head around a few base concepts. Any comments or
criticisms on the below definitions would be most welcome!

A function is an object.

Correct. It is an object, that internally implements the "[[call]]"
method.
JavaScript objects have properties:
- a var (which is an object or a primitive);

Don't call it a "var". That keyword is used for declaring local
variables, not properties of objects.
- a method (which is a function assigned to the object).

It is a property with a value that is a function. We can call it a
"method" if we want.
Note that since methods are functions and functions are objects we
could re-write the above to reflect a different view of an object's
properties.

JavaScript objects have properties:
- other objects (declared as a var or method);

Just "other objects (functions and non-functions)".
- primitive vars.

A different approach is:

A Javascript *value* is either a primitive value (number, string,
boolean, undefined or null), or it is an *object*.

*Objects* have properties, accessible by name, which contain
*values*.

Some *objects* are *functions*, and can be called.

/L
 
R

Robert Bram

Hi All!

I want to thank both David and Lasse for your input to this thread. Here
is the result of my thinking now - once again I welcome further comments
or criticisms.

============
Here are two ways to think about JavaScript objects.

1) The first is in terms of typical object oriented concepts. An object
encapsulates.
- data (called properties in JavaScript); and
- behaviour (called methods in JavaScript).

This way of thinking is useful because it enables a discussion of
objects in a non-language specific manner.

2) The second way to think about JavaScript objects involves us
understanding that in JavaScript, a function is also an object.

JavaScript objects have properties that we can refer to by name.

Each property has a value.

A Javascript value is either:
- a primitive (number, string, boolean, undefined or null); or
- an object (function or non-function).

This way of thinking is useful because it helps us to understand that a
JavaScript object definition (i.e. a class in object oriented
terminology) is any function that assigns itself properties.


Further concepts.

A prototype is an object property that belongs to a constructor function
and is automatically created when the function begins assigning
properties to itself. The prototype is JavaScript's way of remembering
what properties an object definition has. Object definitions (i.e.
constructor functions) may inherit or copy the prototype of other object
definitions - meaning that a constructor may define itself as having the
same set of properties as another constructor.

A var is a local variable. A local variable is a value that may only be
referenced by code within a function or script, depending on whether the
var was declared within a function or script respectively.
============

Rob
:)
 
R

Robert Bram

Hi All!

I want to thank both David and Lasse for your input to this thread. Here
is the result of my thinking now - once again I welcome further comments
or criticisms.

============
Here are two ways to think about JavaScript objects.

1) The first is in terms of typical object oriented concepts. An object
encapsulates.
- data (called properties in JavaScript); and
- behaviour (called methods in JavaScript).

This way of thinking is useful because it enables a discussion of
objects in a non-language specific manner.

2) The second way to think about JavaScript objects involves us
understanding that in JavaScript, a function is also an object.

JavaScript objects have properties that we can refer to by name.

Each property has a value.

A Javascript value is either:
- a primitive (number, string, boolean, undefined or null); or
- an object (function or non-function).

This way of thinking is useful because it helps us to understand that a
JavaScript object definition (i.e. a class in object oriented
terminology) is any function that assigns itself properties.


Further concepts.

A prototype is an object property that belongs to a constructor function
and is automatically created when the function begins assigning
properties to itself. The prototype is JavaScript's way of remembering
what properties an object definition has. Object definitions (i.e.
constructor functions) may inherit or copy the prototype of other object
definitions - meaning that a constructor may define itself as having the
same set of properties as another constructor.

A var is a local variable. A local variable is a value that may only be
referenced by code within a function or script, depending on whether the
var was declared within a function or script respectively.
============

Rob
:)
 
R

relaxedrob

Hi All!

I want to thank both David and Lasse for your input to this thread. Here
is the result of my thinking now - once again I welcome further comments
or criticisms.

============
Here are two ways to think about JavaScript objects.

1) The first is in terms of typical object oriented concepts. An object
encapsulates.
- data (called properties in JavaScript); and
- behaviour (called methods in JavaScript).

This way of thinking is useful because it enables a discussion of
objects in a non-language specific manner.

2) The second way to think about JavaScript objects involves us
understanding that in JavaScript, a function is also an object.

JavaScript objects have properties that we can refer to by name.

Each property has a value.

A Javascript value is either:
- a primitive (number, string, boolean, undefined or null); or
- an object (function or non-function).

This way of thinking is useful because it helps us to understand that a
JavaScript object definition (i.e. a class in object oriented
terminology) is any function that assigns itself properties.


Further concepts.

A prototype is an object property that belongs to a constructor function
and is automatically created when the function begins assigning
properties to itself. The prototype is JavaScript's way of remembering
what properties an object definition has. Object definitions (i.e.
constructor functions) may inherit or copy the prototype of other object
definitions - meaning that a constructor may define itself as having the
same set of properties as another constructor.

A var is a local variable. A local variable is a value that may only be
referenced by code within a function or script, depending on whether the
var was declared within a function or script respectively.
============

Rob
:)
 
L

Lasse Reichstein Nielsen

This way of thinking is useful because it helps us to understand that a
JavaScript object definition (i.e. a class in object oriented
terminology) is any function that assigns itself properties.

In Javascript, the word "class" is, like "method", only something that
exists in the eyes of the user. Internally, a method is just a property
that happens to be a function. And "class" is usually used about a function
that happens to be used as a constructor.

Examples of built-in constructor functions: Object, Array, Number,
String, Date, RegExp. These are *functions*, but if you use them with
the "new" operator, they generate objects that have similar
structures. The notation ("new Foo()") and use (creating new similar
objects) make us think about them as we would about classes in
class-based object oriented languages. But in Javascript, any function
can be a constructor/class.

Example:
function Point(x,y) {
this.x=x;
this.y=y;
}
This function is meant to be a constructor. When you use it as
new Point(10,20)
the "new" operator creates a new object, and calls Point so that the
"this" keyword refers to the new object. It calls Point just as any
other function.

You could get the same result with "Point.call(new Object(),10,20)",
(in this case only, there are more details to the "new" operator).

You can write
new func()
for any function func.
A prototype is an object property that belongs to a constructor function

I.e., any function. Any Javascript function initially has a property
called "prototype", that refers to an object.
and is automatically created when the function begins assigning
properties to itself.

It is created when the function is.

var foo = function(){};
alert(typeof foo.prototype);
The prototype is JavaScript's way of remembering
what properties an object definition has. Object definitions (i.e.
constructor functions) may inherit or copy the prototype of other object
definitions - meaning that a constructor may define itself as having the
same set of properties as another constructor.

This got me confuzed. Constructor functions (i.e., any function) uses
their prototype object for properties that will be shared by the
created objects. They are not properties of the constructor itself.

Any user-created Javascript object has an prototype reference. When
you look for a property, say "obj.prop", then the property is looked
for in the object itself. If it is not found, it is looked for in the
object's prototype, and so forth.

When you create a new object with the "new" operator and a function,
the new object's prototype reference will point to the value of the
function's prototype property.

Try looking at what happens here:
---
function Foo(){};
Foo.prototype.x = 42;
var x = new Foo();
alert(x.x); // 42

x.x = 37;
alert(x.x); // 37

delete x.x; // remove the property from the object
alert(x.x); // 42 again, the prototype wasn't affected.

Foo.prototype.x = 37;
alert(x.x); // 37. The prototype object can be changed dynamically too.

Foo.prototype = {x:4,y:87}; // overwrite prototype with new object
var y = new Foo();

alert(x.x); // 37, the prototype reference of old objects are unchanged
alert(y.x); // 4
alert(y.y); // 87
 
L

Lasse Reichstein Nielsen

Robert Mark Bram said:
This is reminiscent of the Matrix! Let me see if I understand right ...

"There is no function^H^H^H^H^H^H^H^Hspoon".
function Bar(){
this.prototype;
}
Bar.prototype;

OK then.. Bar is an object - actually it is an instance of Function.

Yes. Its prototype reference (not to be confuzed with its property called
"prototype") refers to Function's "prototype" property's value.

Proof:
var result = "";
var x = function() {}; // create a function
result += x.foo; // see that its "foo" property is undefined
Function.prototype.foo = 42; // add a foo property to Function.prototype
result += "/"+x.foo; // see that x's "foo" property is now defined
alert(result);
Function defines a property called "prototype". This is why Bar.prototype
works.

The prototype property of a function is *not* there because there is a
Function.prototype.prototype
There isn't. A newly created function is assigned a brand new object
as a prototype property.

If the prototype property came through the prototype reference, then
all functions would share the same objects as their prototype property.
They don't, each function has its own object.
this.prototype doesn't work because "this" is a reference to "a" Bar object.

(well, it "works" and gives "undefined" :)

The "this" in Bar will most likely refer to the global object, which don't
have a prototype property.
The Bar object is not a Function and therefore does not have a prototype.

Add "probably" before "does not". :)
But otherwise, yes.
How does that sound?

Almost correct. :)
/L
 
D

Douglas Crockford

This is reminiscent of the Matrix! Let me see if I understand right ...
function Bar(){
this.prototype;
}
Bar.prototype;

OK then.. Bar is an object - actually it is an instance of Function.
Function defines a property called "prototype". This is why Bar.prototype
works.

this.prototype doesn't work because "this" is a reference to "a" Bar object.
The Bar object is not a Function and therefore does not have a prototype.

There are actually two prototype properties. Some object will have both.

First, all functions have a .prototype object, which is usually a container of
instance methods. All functions have one because any function could potentially
be a constructor. JavaScript does not distinguish between functions and
constructors, which is a minor problem. That means that you must use 'new' when
calling a constructor.

Second, all objects have an invisible [[proto]] member. If a search for a name
in an object fails, the name will then be sought again from the [[proto]]
object. If that fails, it will search the [[proto]] object's [[proto]] object,
and so on. This supports the reuse pattern in JavaScript.

The [[proto]] member is set at the creation on the object and cannot be changed
or directly examined. (Netscape 4 does allow access to __proto__, but this is
non-standard.) The 'new' unary operator makes a new empty object with [[proto]]
set to constructor.prototype, then binds it to this while calling the
constructor function.

It is almost always wrong to refer to this.prototype .

http://www.crockford.com/#javascript
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,219
Latest member
Geraldine7

Latest Threads

Top