The author writes this JavaScript article tutorial for Flash ActionScript developers that have the same interest - like, work with Flash and ActionScript but still want to learn some things new in JavaScript programming language.
Although this JavaScript article tutorial is written for ActionScript programmers, but it is also a very good tutorial for who to learn object-oriented JavaScript programming. This JavaScript article tutorial guides you how to build a full-completed JavaScript object with the getter and setter methods. Please visit for more details.
Other related JavaScript OOP article tutorials:
- JavaScript OOP Tutorial for Dummies
- Pointers in OOP JavaScript
- OOP in JavaScript: Some Basics
- OOP JavaScript: Public and Private Methods
- Basic OOP Concepts in Javascript
- Demo
- Enlarge
- Reload
- New window
Free iPage Web Hosting for First Year NOW
If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?
Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
Anyway, there are (ofcourse) some techniques to code like Actionscript in JavaScript. These snippets have helped me to understand object oriented JavaScript. For too long I did not even tried to learn what was possible with JavaScript because I thought it was a very limited language. Maybe some information is outdated; I am still learning, so please share your feedback.
Object oriented javascript
In javascript it is possible to create classes, public and private vars, getters/setters and there are even constructors. A function could be seen as a class. This is a bit strange at first, but if you use it for a while you will understand. Now lets create our own class right now.
Lets port some stuff of this Color Class to javascript. It has private vars and public functions.
Declare classes
Defining classes is as easy as defining functions, because it is almost the same. After creating an function, you could already make some instances of it. The example below creates a Color class with a public variable called value. After that we create 2 instances of the Color class with some other values.
Note: I use the same code conventions as Actionscript: Classes should be UpperCased, functions/vars should be lowerCased and constants should be FULL_CAPS. Oh and since javascript looks like as1.0, I sometimes use an underscore before private vars
{
this.value = value || 0;
alert("Color created with value: " + value)
}
var myRedColor = new Color(0xFF0000); // create instance of Color (a red one)
var myOrangeColor = new Color(0xFFCC00); // create instance of Color (an orange one)
alert( myRedColor.value );
alert( myOrangeColor.value );
Public Private
In actionscript it is very useful to use encapsulation, which is a mechanism for restricting access to other objects. The previous+following example should work in all browsers. The difference between public and private vars/functions in javascript can be declared like this:
{
// public variable
this.value = value || 0xFFFFFF; // set default value to 0xFFFFFF for parameter if it isn�t defined
// private variable
var _name = "test";
// public function
this.getRandomColor = function( )
{
return Math.random() * 0xFFFFFF;
}
// private function
function getNiceColor()
{
return 0xffcc00;
}
}
// create instance of Color
var color = new Color(0xFF0000);
alert( color.value ); // returns red color
alert( color.getRandomColor() ); // returns random color
// not possible :
alert( color.getNiceColor() ); error in console; property does not exist, because function is private.
alert( color._name ); // error in console; property does not exist, because variable is private.
I think with this principles (maybe in combination with namespaces, see below) you can create clean javascript code.
Packages Namespaces
Now before using classes in javascript I had some conflicts with function and variable names because there were unwanted duplicates. I have written a little helper tool to use namespaces. It helps to prevent those conflicts. Now it looks more like Actionscript 3, only there is no such thing like �imports�. This snippet works in all browsers.
function Namespace(namespace)
{
var parts = namespace.split(".");
var root = window;
for(var i = 0; i < parts.length; i++)
{
if(typeof root[parts[i]] == "undefined")
{
root[parts[i]] = {};
}
root = root[parts[i]];
}
}
// creating my own namespace here
Namespace("nl.stroep.utils");
nl.stroep.utils.Color = function(value) // create class inside package
{
this.value = value || 0xFFFFFF;
}
var myRedColor = new nl.stroep.utils.Color(0xff0000);
alert(myRedColor.value);
- Sent (0)
- New
Generate your business videos by AI with voice or just text
chatGPTaz.com
Talk to ChatGPT by your mother language
AppAIVideo
Your first FREE AI Video App
Deepfake Video
Deepfake AI Video Maker
Deepfake
Deepfake AI Video Maker
AI Deep Fake
Deepfake AI Video Maker
AIvidio
AI Video Mobile Solutions
AIvideos
AI Video Platform & Solutions
AIvedio
AI Video App Maker
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing
Dall-E-OpenAI.com
Generate creative images automatically with AI
chatGPT4.win
Talk to ChatGPT by your mother language
First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language
Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools
GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project
iPhoneKer.com
Save up to 630$ when buy new iPhone 16
Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k