00:00
00:00
Newgrounds Background Image Theme

mariobros22 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Javascript - Objects (AS3ish)

727 Views | 10 Replies
New Topic Respond to this Topic

Javascript - Objects (AS3ish) 2015-08-15 14:27:39


I've been using code academy and I've got this

function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
  
   var returnBalance = function() {
      return bankBalance;
   };
   
   this.askTeller = function(){  
       return returnBalance;
   }
}

var john = new Person('John','Smith',30);
var myBalanceMethod = john.askTeller();
var myBalance = myBalanceMethod();
console.log(myBalance);

This works and returns 7500. At the end we have this:

var myBalanceMethod = john.askTeller();
var myBalance = myBalanceMethod();

If myBalanceMethod = john.askTeller();
then surely
myBalance = john.askTeller();

Then why when I have the following:

var myBalanceMethod = john.askTeller();
console.log(myBalanceMethod);
var myBalance = myBalanceMethod();
console.log(myBalance);

I get these printed

[Function]
7500

Which is fair enough, but how does putting it in a myBalance variable stop it from being displaying function? When the log only picks it up as function. Especially since myBalanceMethod is called with parentheses.

On a side note, why when I try this code in AS3, I get

var john = new Person('John','Smith',30);
1048: Method cannot be used as a constructor.

I thought this was possible?

Response to Javascript - Objects (AS3ish) 2015-08-15 16:18:28


At 8/15/15 02:27 PM, Aprime wrote: I get these printed

[Function]
7500

Which is fair enough, but how does putting it in a myBalance variable stop it from being displaying function?

It's not because of the variable you stored it in, it's because you stored the result of john.askTeller(), which is a function, into that variable and then called the function.

If you do this, you'll get [Function] twice:

var myBalanceMethod = john.askTeller();
console.log(myBalanceMethod);
var myBalance = myBalanceMethod;
console.log(myBalance);

myBalanceMethod is a reference to your returnBalance function inside of the Person prototype and askTeller returns that reference.

When the log only picks it up as function. Especially since myBalanceMethod is called with parentheses.

At 8/15/15 02:27 PM, Aprime wrote: On a side note, why when I try this code in AS3, I get

var john = new Person('John','Smith',30);
1048: Method cannot be used as a constructor.

I thought this was possible?

You need to create a class in AS3; you can't use function prototypes to create objects.

Response to Javascript - Objects (AS3ish) 2015-08-15 17:08:39


At 8/15/15 04:18 PM, Diki wrote:
At 8/15/15 02:27 PM, Aprime wrote: You need to create a class in AS3; you can't use function prototypes to create objects.

A little surprised it worked in AS2, but it did (no need to explain why).

Umm, in regards to AS3.

You say I need a class for this, how shall I write this to get it to work? A little confused

Response to Javascript - Objects (AS3ish) 2015-08-15 17:32:53


At 8/15/15 05:08 PM, Aprime wrote: A little surprised it worked in AS2, but it did (no need to explain why).

It's basically just because AS2 is closer to ECMAScript than AS3 is, which both languages are dialects of.

At 8/15/15 05:08 PM, Aprime wrote: Umm, in regards to AS3.

You say I need a class for this, how shall I write this to get it to work? A little confused

First, I'd say read up on classes. But your Person class could look something like this:

public class Person {
   public var firstname:String;
   public var lastname:String;
   public var age:int;

   private var bankBalance:int;

   public function Person(first:String, last:String, age:int) {
      firstname = first;
      lastname = last;
      age = age;
   }
}
var john:Person = new Person("John", "Smith", 30);

Response to Javascript - Objects (AS3ish) 2015-08-15 18:51:49


At 8/15/15 05:32 PM, Diki wrote: First, I'd say read up on classes.

I think I also need to read up on packages too

But your Person class could look something like this:

For now, is there any way I can do this without classes? In a near as close enough way to the Javascript version as possible. Mostly for comparison reasons.

Response to Javascript - Objects (AS3ish) 2015-08-15 19:32:29


At 8/15/15 06:51 PM, Aprime wrote: For now, is there any way I can do this without classes? In a near as close enough way to the Javascript version as possible. Mostly for comparison reasons.

Simply use As1 then.

The definition of classes via the keyword class in external files (very similar to the way it is in As3 now) got introduced in As2.

The javascript way of defining classes via functions (and prototypes) got more and more deprecated ever since. (not generally, but in the realm of actionscript)

actionscript grew into a more strict and strongly typed language, trying to compile javascript as As3, is not necessarily a good idea, if you really want to do it, do so in an As2 file.

As it looks like now, ES6 will introduce keywords like "class", possibly comparable to the way As2 introduced them to As1.

Response to Javascript - Objects (AS3ish) 2015-08-16 12:12:13


Diki described the error in the JS; to make object methods and properties, you need to attach it with this:

function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
this.bankBalance = 7500;

this.askTeller = function() {
return this.bankBalance;
};

this.depositMoney = function(deposit) {
this.bankBalance += deposit;
};

this.withdrawMoney = function(withdraw) {
this.bankBalance -= withdraw;
};

};

var john = new Person('John','Smith',30);
console.log(john.askTeller());
john.depositMoney(500);
console.log(john.askTeller());
john.withdrawMoney(200);
console.log(john.askTeller());

This returns:

7500
8000
7800

I hate JavaScript's lack of classes...

Response to Javascript - Objects (AS3ish) 2015-08-16 16:08:28


At 8/16/15 12:12 PM, Lictalon wrote: I hate JavaScript's lack of classes...

I've decided to paste your code into AS3, just to get it working there as well.

function Person(first,last,age) {
	this.firstname=first;
	this.lastname=last;
	this.age=age;
	this.bankBalance=7500;
	
	trace(this.firstname);

	this.askTeller = function() {
		return this.bankBalance;
	};

	this.depositMoney = function(deposit) {
		this.bankBalance += deposit;
	};

	this.withdrawMoney = function(withdraw) {
		this.bankBalance -= withdraw;
	};

}

Person('John','Smith',30);

At the bottom, I've completely got rid of all those functions, for now.

My trace on line 7 works, of course.

Is there a way for me to reference the name from outside? Or is that also another impossible task?

Are the functions even reference-able from the outside?

I've tried both

trace(Person.askTeller());
trace(Person.firstname);

To clarify, is this something that is not possible? And is it not possible to have a new version of Person called something else, without classes?

Response to Javascript - Objects (AS3ish) 2015-08-16 18:31:04


At 8/16/15 04:08 PM, Aprime wrote: To clarify, is this something that is not possible? And is it not possible to have a new version of Person called something else, without classes?

There is indeed something to clarify here: Person is a class. No matter if you do it in the javascript function style or the actionscript external class file style.

A class is a definition that defines how objects (that are derived from that class) should be, which means what properties and methods they have.

So can we please stop this "...but without classes?" thing? Because the only motivation to do this is to create a class, which in javascript land is a function (any function in fact), which in turn is used to create objects and the creation of objects is why we are doing all that.

At 8/16/15 12:12 PM, Lictalon wrote: I hate JavaScript's lack of classes...
I've decided to paste your code into AS3, just to get it working there as well.

function Person(first,last,age) {
this.firstname=first;
this.lastname=last;
this.age=age;
this.bankBalance=7500;

trace(this.firstname);

this.askTeller = function() {
return this.bankBalance;
};

this.depositMoney = function(deposit) {
this.bankBalance += deposit;
};

this.withdrawMoney = function(withdraw) {
this.bankBalance -= withdraw;
};

}

Person('John','Smith',30);

There are plenty of "this" in this code, which all refer to the current object instance that is being created as the function Person is executed. (this isn't actually true in some parts, but within the scope of this answer it is) "this" is also what the function Person returns.

Is there a way for me to reference the name from outside? Or is that also another impossible task?

Who's name? You specified in your class Person that all objects derived from that class have a firstname property indeed.
But as the entire point of all of this is to work with objects, you need a reference to an object in order to interact with an object.

Properties and methods have to be called on an object, which makes an object necessary in the first place.

To create a new object, use the new keyword and by all means, keep a reference to the object you are creating, otherwise this is all useless:

Person('John','Smith',30);

should be:

var john = new Person('John','Smith',30);

So there you have your object in variable to do whatever you want with it.

Are the functions even reference-able from the outside?

I've tried both

trace(Person.askTeller());
trace(Person.firstname);

You never created Person.askTeller() or Person.firstname.

Recall that "this" within the Person class refers to the object being created. You do add the property firstname and the method askTeller(), but you add them to "this", so they end up on "this" and not Person, which is why you cannot find them on Person and they are not referenceable on Person.

If you want to get the name of the previously created object, access the property of the object via the reference to the object that you created upon its creation, namely "john", full code:

var john = new Person('John','Smith',30);
trace(john.firstname);

Recap:

- in oop, we want to deal with objects, hence the name
- we have to come up with some way to define what an object looks like (what properties and methods -or members, for short- it has) and for the sake of keeping communication simple let's call that definition a class.
- we call "new class-name" to create a new object according to what we defined in that class
- we need to reference objects in order to interact with them, a variable can do that just fine
- we access members of an object with the dot syntax: "objectreference.member", as in "objectreference.property = value;" or "objectreference.method(value);"

does that help?

Response to Javascript - Objects (AS3ish) 2015-08-21 12:19:05


At 8/16/15 06:31 PM, milchreis wrote:
At 8/16/15 04:08 PM, Aprime wrote: Person('John','Smith',30);
should be:

var john = new Person('John','Smith',30);

It was like in my first post, but it throws an error. I cannot get it to work without external classes. Ideally, even though not the best method, I'd like it to work within a single fla file.


Fixed it

function CreatePerson(first, last, age)
{
	function returnBalance(){
			return bankBalance;
		}
	var bankBalance = 7500;
	return {
		firstname: first,
		lastname: last,
		age: age,
		returnBalance: returnBalance,
		askTeller: function() : Function{
			return returnBalance;
		}
	};
}
var john = CreatePerson('John', 'Smith', 30);
var myBalanceMethod = john.askTeller();
var myBalance = myBalanceMethod();
trace(john.askTeller()());
trace(john.age);
trace(myBalance);

Does practically the same thing.