473,762 Members | 4,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1967
re********@optu snet.com.au (re********@opt ushome.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********@opt usnet.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*********@yo urshoesinfotech .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.protot ype.foo = 42; // add a foo property to Function.protot ype
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.protot ype.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.pro totype, 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
3348
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 charcters in it, that the code needs to run until all bad characters are gone? For example, if a file has the name "<bad*mac\file" the program has to run 3 times to get all three bad chars out of the file name. The passes look like this:
99
5918
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 important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the changes unecessary to this discussion. I left in the change of "instance variable" syntax (...
6
2026
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 code: #def put_file(file_id, delete=False): # """ Function to put the file on the FTP Server # """ # print " FTP for this file started"
1
741
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 managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form...
27
2674
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
45522
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 defined in "A.html", but every attempt results in an "is not a function" error. I have tried to invoke the function using parent.document.funcname(), top.document.funcname(), and various other identifying methods, but all result in the above...
23
4019
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 who will find them very helpfull. gcc has them as a non-standard option, but only when compiling C language code, so I'm afraid there might be some obscure reason why local functions are not so easy to be dealt with in C++, which I do not yet know.
47
3890
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 say, C programs consist of variables to store the input and functions to manipulate them. This would make C object-oriented - how cool would that be? Are there any problems with adding the ability to have functions
14
6024
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 static inside functions (i.e. local static objects) 5. objects declared at file scope.
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9927
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5268
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3914
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.