473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Functions as objects

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
:)
Jul 20 '05 #1
9 1951
re********@optusnet.com.au (re********@optushome.com.au) writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
> 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

Jul 20 '05 #3
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
:)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #4
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
:)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
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
:)
Jul 20 '05 #6
Robert Bram <re********@optusnet.com.au> writes:

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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7
Hi Lasse!
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.


Good. I understand this now.
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.


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.

How does that sound?

Rob
:)
Jul 20 '05 #8
"Robert Mark Bram" <ro*********@yourshoesinfotech.monash.edu.au> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #9
> 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

Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
6
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
47
by: Albert | last post by:
So structures are useful to group variables, so you can to refer to a collection as a single entity. Wouldn't it be useful to also have the ability to collect variable and functions? Ask K&R...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.