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?