- Collaboration (“uses a”): An object uses the features of another object to accomplish a task. For example, a Customer Repository “uses a” Customer object to populate on a Retrieve and serialize on a save.
- Composition (“has a”): An object can be composed of other objects. For example, an Order “has a” customer, and an Order “has a” shipping address.
- Aggregation: A special type of composition whereby the component parts do not exist except as part of the composition. For example, the Order and Order Item relationship is an aggregate because the Order Item does not exist except when it is associated with a specific Order.
- Inheritance (“is a”): An object “is a” type of another object. For example, a Business Customer “is a” Customer, and a Residential Customer “is a” Customer.
Class Relationship Types in Object Oriented Programming | OOPS
Object Oriented Programming in JavaScript
We can achieve Object Oriented Programming in JavaScript with a little effort.
Creating an Object in JavaScript
There are two ways to create a JavaScript object. They are Constructor functions
and Literal notation
.
function myObject(){ };Literal Notation
var myObject = { };
Literal notation is a preferred option so that the JavaScript code doesn't interfere with other scripts running on the page and also if you are using this object as a single object and not requiring more than one instance of the object; whereas Constructor function type notation is preferred if you need to do some initial work before the object is created or require multiple instances of the object where each instance can be changed during the lifetime of the script.
Defining Methods and Properties in JavaScript ObjectLet us create an object with a property named myProperty and a method named myMethod.
Constructor Functionsfunction myObject(){ this.myProperty = 'my property'; this.myMethod = function(){ alert('This is ' + this.myProperty); }; };
The constructor object has its properties and methods defined with the keyword ‘this’ in front of it. Properties/methods have their ‘values’ defined after an equal sign ‘=’. An optional semi-colons ‘;’ at the end of each property/method declaration.
A constructor functions need to be instantiated to use the object as below.
var myNewObject = new myObject(); myNewObject.myMethod();Literal Notation
var myObject = { myProperty : 'my property', myMethod : function(){ alert('This is ' + this.myProperty); } }
The properties/methods have their ‘values’ defined after a colon ‘:’. If you have more than one property or method, they MUST be separated with a comma ‘,’, and they CANNOT have semi-colons after them, otherwise JavaScript will return an error.
A literally notated object simply refered by its variable name as below.
myObject.myMethod();
Popular Tutorials
- Creating Cookie in ASP.NET MVC Action | Handling Cookies in ASP.NET MVC | Set Cookie Expiry in ASP.NET MVC | ASP.NET MVC Tutorial
- Generating Multiline TextBox or TextArea with @Html.EditorFor in ASP.NET MVC
- Generating Unique Token in C# | Generating Unique Token that Expires after 24 Hours in C# | C# Tutorial
- Drag & Drop File Upload In ASP.NET MVC Using dropzone js with Fallback Browser Support | ASP.NET MVC Tutorial
- Loading PartialView Via JQuery In ASP.NET MVC | Returning PartialView From ASP.NET MVC Action | ASP.NET MVC Tutorial
- How To Enable Role Manager Feature In ASP.NET MVC? | ASP.NET MVC Interview Question
- How To Add CSS Class And Custom Property in Html.TextBoxFor? | ASP.NET MVC | RAZOR
- Send and Receive SMS and MMS Messages Using Android 4.4 SMS API | Android Video Tutorial
- How to Get Browser Agent from ASP.NET Web API Controller? | ASP.NET Web API Tutorial
- How to Override the Common Route Prefix? | ASP.Net MVC Interview Question