Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #24579 > unrolled thread
| Started by | bit-naughty@hotmail.com |
|---|---|
| First post | 2014-06-02 11:41 -0700 |
| Last post | 2014-06-03 06:03 -0700 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.javascript
OOP and first class functions question bit-naughty@hotmail.com - 2014-06-02 11:41 -0700
Re: OOP and first class functions question Scott Sauyet <scott.sauyet@gmail.com> - 2014-06-02 12:25 -0700
Re: OOP and first class functions question bit-naughty@hotmail.com - 2014-06-03 06:03 -0700
| From | bit-naughty@hotmail.com |
|---|---|
| Date | 2014-06-02 11:41 -0700 |
| Subject | OOP and first class functions question |
| Message-ID | <8c4d0f0d-6532-4865-a63e-89eaba292f3e@googlegroups.com> |
If I have var x = function() { whatever}, and I then say var p =x , can I then say var newobj = new p(); ? And then will everything inside x be available like eg. newobj.method?
Thanks.
[toc] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2014-06-02 12:25 -0700 |
| Message-ID | <33412217-1847-423b-8a6c-f1b379986b0c@googlegroups.com> |
| In reply to | #24579 |
bit-naughty@hotmail.com wrote:
> If I have var x = function() { whatever}, and I then say var p =x ,
> can I then say var newobj = new p(); ? And then will everything
> inside x be available like eg. newobj.method?
Yes.
var Rectangle = function(w, h) {this.width = w; this.height = h;};
Rectangle.prototype.area = function() {return this.width * this.height;};
var Rect = Rectangle;
var box = new Rect(3, 5);
box.area(); //=> 15 // It works!
The function is a first-class item in the Javsacript ecosphere. The variable
`Rectangle` used to hold a reference to that function is just that. You can
add another reference, such as `Rect`, and that function will still behave
as before. You could even then reset the original reference, and the new one
would continue to function:
Rectangle = null;
var r = new Rectangle(2, 6); //=> TypeError
var r = new Rect(2, 6);
r.area(); //=> 12
HTH,
-- Scott
[toc] | [prev] | [next] | [standalone]
| From | bit-naughty@hotmail.com |
|---|---|
| Date | 2014-06-03 06:03 -0700 |
| Message-ID | <79b41351-e3aa-4781-b956-2d44e89dc547@googlegroups.com> |
| In reply to | #24580 |
Thanks, Scott! :)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web