Wrox Programmer Forums
|
Intro Programming What is a loop? Which language is best for beginners? What is "object oreinted?" All those types of questions and more are welcome here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Intro Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 28th, 2008, 06:44 PM
Registered User
 
Join Date: May 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Object Type in JavaScript

I will start with a definition of what I believe a data type is;

Data Type A description of how expressions are characterised internally and how they can be combined and manipulated.

My question is about the phrase 'loosely typed'; a lot of references describe this in terms of variables not needing to have their type specified.

If we look at JavaScript, functions can be passed as values, and can also behave like 'classes' (I am aware that JavaScript is class-free and uses Prototypal Inheritance etc.)

Is it correct to say that since JavaScript is loosely typed then the statement above holds.

If not, then why can functions behave like 'classes' or be passed as values?

Thank you,
YMas
Reply With Quote
  #2 (permalink)  
Old May 29th, 2008, 09:45 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I'd define a data type as something like "a datum or set of data with values having predefined characteristics" for example floating point, integer numbers, structs or classes.

I'll try and explain loosely typed by looking first at strongly typed. Strongly typed generally means that there's some kind of checking of types to make sure that you don't trip over yourself and use the wrong type. In a mathematical function it would be better for it to blow up when you pass it a string than for it to try and add a string to an integer. There's no reason why you can't pass an object (i.e. instance of a class) to a function but in a strongly typed language the interpreter or compiler will make sure that you've passed an instance of the correct class and die if you haven't.

Here's some code in java - a strongly-typed language
Code:
public class type{
  private int x;

  public type(int y) {
    this.x=y;
  }

  public int squared () {
    return (this.x * this.x);
  }

  public static void main(String argv[]) {
    type t = new type(4);
    System.out.println(t.squared());
  }
}
This compiles happily and prints 16 to the screen. If we change:
Code:
type t = new type(4);
to:
Code:
type t = new type("House");
The the java compiler blows up like this:
Code:
charlie@charlie:~/src/java$ javac type.java 
type.java:13: cannot find symbol
symbol  : constructor type(java.lang.String)
location: class type
        type t = new type("House");
                 ^
1 error
charlie@charlie:~/src/java$
This is considered a good thing by some programmers who are afraid of their types getting in a muddle.

In Perl, a loosely typed language we could write something similar (I use a function rather than a class for conciseness).
Code:
#!/usr/bin/perl

sub squared($) {
  my $x=shift;
  return $x+$x;
}

print squared(4) . "\n" . squared("House") . "\n";
Perl silently evaluates the String as 0 and prints
Code:
8
0
See also http://blog.jeremymartin.name/2008/0...typing-in.html

Cheers


--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
  #3 (permalink)  
Old May 29th, 2008, 04:40 PM
Registered User
 
Join Date: May 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ciderpunx,
Thank you for your reply. Very much appreciated.

As you correctly point out above, the Java compiler will throw an exception if the argument of the function squared() is not an integer, whereas PERL will dynamically convert the string object into an integer and evaluate -- a consequence of loose typing.

In JavaScript, this would be an example of a constructor:
Code:
  function Car(color, type) {
    this.color = color;
    this.type = type;
  }
provided I invoke it like this:

Code:
  car1 = new Car('blue','SUV');
car1 is a reference to an object that behaves as the value of the "this" keyword.

I thought that this was a consequence of loose typing on the object type (I believe this is completely incorrect).

I can now see that this is a consequence of the way JavaScript was written i.e. it is a feature of JavaScript. Functions are data types and not just syntax.

Many thanks for your time.

Regards,
YMas
Reply With Quote
  #4 (permalink)  
Old May 29th, 2008, 07:24 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmm. I am not sure I understand your statement:

"car1 is a reference to an object that behaves as the value of the "this" keyword."

First and formost this in JavaScript is used to denote what object owns a given item (function, variable, etc) as you have pointed out in the first part of your sentence but car1 is not a reference to an object that behaves like "this" (unless my understanding of the ECMA spec is incorrect) , it is an instance of your Car object thus pointing to that object.

Charlie, that was an excellent explination.

hth.
-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #5 (permalink)  
Old May 30th, 2008, 06:58 AM
Registered User
 
Join Date: May 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Doug,
Thank you for your reply, very much appreciated.

My understanding of the "new" operator is that it performs the following tasks:
  • It creates an object with no members.
  • It calls the constructor for the object ( Car in my example above). It then passes a pointer to the newly created object as the "this" pointer.
The constructor then initialises the object. In the example above, it will give the car1 object a color and a type [of vehicle]. Am I correct to assume that this means; car1.color will be 'blue' and car1.type will be 'SUV'.

Car has been passed a reference to a newly created, "empty" object as the value of the "this" keyword. Car is then responsible for initialising the car1 object.

Quote:
quote:car1 is not a reference to an object that behaves like "this"
Sorry, this isn't what I meant. I think that for a function to be classified as a constructor (15.3.2, ECMA-262, 3rd edition) in JavaScript; it needs to be
  • Invoked by the new operator
  • Passed the address of a newly created object through the "this" keyword.
Is it incorrect to assume that car1 is this reference? If not, then how does the "new" operator pass the address of the new object to the constructor?

It is very likely that my understanding is still incorrect.

Any comments are very welcome and greatly appreciated.

Many thanks,
YMas

P.S. Charlie, thank you for your clear explanation of loose-typing.
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to cast object of type 'TheBeerHouseSection DyerOppenheimer BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 4 June 10th, 2008 12:13 PM
Unable to cast object of type galua SQL Language 3 January 9th, 2008 01:31 PM
Data type for unbound object !!! vubinhsg BOOK: Access 2003 VBA Programmer's Reference 0 September 14th, 2006 04:10 PM
inserting image data type object MAKO SQL Server 2005 1 September 8th, 2006 04:44 PM
OLE Object Type? [email protected] Access VBA 1 October 20th, 2004 02:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.