Saturday, October 4, 2014

Objects: Lecture 1. What are Objects?



What are objects?



Objects are an extremely important topic in many modern programming languages, as well as in JavaScript. They dramatically increase your programming power, richness, effectiveness and efficiency. There are many tutorials on any part of JavaScript and you are encouraged to browse through some of them in order to find the ones that explain a particular topic in a way best for your learning methods and current understanding. Here I talk about something that will allow you to get into the tutorials on objects easier, compensating for often neglected efforts in letting students understand the nature of objects deep enough to be more comfortable with specific explanations.


Objects are complex data types that might contain data (properties) and a bunch of small programs (methods). Therefore they are like combinations of files with data and a library of programs in one system. Such system can be activated by just its name, can be send somewhere as one thing and then used in all its aspects, like getting data (properties) stored in it and/or running some programs that are contained in objects. In comparison with having files and programs separate – such complex systems (data types) like objects can be easily created and handled as one universal package where we can use its functional capabilities (by running the programs), its data storage capabilities by adding, changing, and retrieving data, or by both at the same time when newly obtained by an object data can be processed by its own programs (methods) either changing its own data or controlling input and output of data processing in and out of an object.
 


Object-oriented analysis, design and programming tends to see any real life object, process, concept, and so on as potential software objects with their structural and behavioral characteristics. Having these features, program objects can model real life objects that usually also have properties and can doo something (or something can be done to them). Examples: a car, a computer, a pen (with its size and weight, as well as an ability to touch paper, leave a line of ink, etc.) and so on. Once you are trained in such vision – you can quickly create models of real situations (or virtual and game situations)  and make them interact with each other using their properties and behaviors in response to initiatives of other objects. Thus you can create real  virtual/game worlds that react to actions of their own objects or physical objects like players.
 


Since your software can activate many different objects for communication with each other – anything that you want object to store, retrieve or do requires the use of a particular object name because otherwise it will be unclear who you are talking to (in your command).  This can be achieved by calling first the name of a particular object followed by the operation on its data or invoking one of its methods (small programs).


ExampleA: myCar. CurrentMileage = 2000;


ExampleB: myCar.showYourCurrentMileage(); 
 


Here the first example shows how to store data in one of the properties of myCar object (CurrentMileage), while the second shows how to run a small program (showYourCurrentMileage()) that might be programmed to get that property and show it on the page. See below an example from w3schools of an object “person” and a link allowing to experiment with it by adding and retrieving more properties.
 


Example 1. Properties:


<body>


<article id="demo"></article>


<script>


var person = {


    firstName : "John",


    lastName  : "Doe",


    age       : 50,


    eyeColor  : "blue"


};


document.getElementById("demo").innerHTML =


person.firstName + " is " + person.age + " years old.";


</script>


</body>


</html>
 


Experiment with it live (http://www.w3schools.com/js/tryit.asp?filename=tryjs_create_object1 ) by adding and retrieving new properties, like making it say: “John is 50 years old and 4 feet tall.”


Then create more interesting objects with properties and something printed  out about that object. Later you will be adding code for changing the properties and adding new properties dynamically.

No comments:

Post a Comment