In our previous article we talked about how to use Namespaces and Nested Namespaces.
As you learned in our previous articles, WinJS lets you build your own structure in an OOP way. Speaking of this, I'd like to move the topic a little bit further and show you how to create Class in WinJS.
Classes are a very important entity in OOP. We can't think of OOP without classes.Classes are the foundation of OOP but not the entire perspective. So let's create some classes and use them in our apps.
Building the App
Note: Building the app from scratch or continuing from an existing project is up to you. But you can create a new WinJS project and add a new js to the project to start working with what we have in mind. We'll be using Universal Windows Apps for showing the demo.
What we're going to create is a Country class that stores 2 properties (Name and Code) which we can assign once we call them:
- var Country = WinJS.Class.define(
- function (name, code) {
- this.name = name;
- this.code = code;
- });
This is the easiest way to create a class. You use the help of WinJS library and apply your own structure.
Class creation process is almost similar according to most languages out there, but to call this class you need to write:
- var country = new Country("Turkey", "TRK");
- console.log("Country Code = " + country.code);
- console.log("Country = " + country.name);
Once you run the project, if you check on Javascript Console, you shall see this:
This was an easy explanation of how classes can be created. But I'd like to give a much more detailed example. Ignore the information entered. They're just an example:
- var Employee = WinJS.Class.define(
- function (name,surname,age,profession,nationality,salary) {
- this.name = name;
- this.surname = surname;
- this.age = age;
- this.profession = profession;
- this.nationality = nationality;
- this.salary = salary;
- });
-
- var Company = WinJS.Class.define(
- function (name,location,interest,employeecount,owner) {
- this.name = name;
- this.location = location;
- this.interest = interest;
- this.employeecount = employeecount;
- this.owner = owner;
- });
We've created two classes named Employee and Company with a set of properties for each of them.Lets create three sample Employees and Company:
- var emp = new Employee(
- "Ibrahim",
- "Ersoy",
- "30",
- "Developer",
- "Turkey",
- 60000);
-
- var emp2 = new Employee(
- "Gul",
- "Ershad",
- "30",
- "Senior Software Developer",
- "India",
- 100000);
-
- var emp3 = new Employee(
- "Dhananjay",
- "Kumar",
- "35",
- "Team Leader",
- "India",
- 200000);
-
-
- var company = new Company(
- "MindCracker LLC",
- "India",
- "Internet",
- 3,
- "Mahesh Chand");
Now let's call them in our app.
- console.log(emp.name + " is a " + emp.age + " years old " + emp.profession + " working at " + company.name);
- console.log(emp2.name + " is a " + emp2.age + " years old " + emp2.profession + " working at " + company.name);
- console.log(emp3.name + " is a " + emp3.age + " years old " + emp3.profession + " working at " + company.name);
- console.log("Owner of " + company.name + " is " + company.owner);
Run the app and check on Javascript Console:
Creating Classes in WinJS is easy as you can see.The knowledge you gained from Server Side OOP can be re-applied in WinJS without headache. Actually its
"almost" the same as creating classes in JavaScript. Here's an example below of how we can create a class in JavaScript:
- class Employee {
- constructor(name,surname,age,profession,nationality,salary) {
- this.name = name;
- this.surname = surname;
- this.age = age;
- this.profession = profession;
- this.nationality = nationality;
- this.salary = salary;
- }
- }
We need to use "
class" and "constructor" terms for creating classes ; while in WinJS you don't need to create a constructor and you don't need to write class term. Everything is already dealt with while defining the class in WinJS.
Our next article will be about deriving classes which is known as Inheritance. So stay tuned and follow my articles.
Read more articles on Universal Windows App: