Classes
var myObject = {
property1 : MyProp,
method1 : function() {
}
property2_Array : [ {arrayelement1 : "one"} {arrayelement2 : "two"} ];
}
A empty object literal
var myObject = { }
This uses object literal notation
var myobject = {
name: "MyName",
age: 20
};
Object Literals
This has 2 members
One property and one method
var myObject = {
Property_Status : "Ready",
Method_DoSomething : function() {
alert('something');
}
}
Declaring
Classes are just special types of functions with additional functionality.
There are two ways you can define a class.
You can either use a declaration or an expression.
Class Declaration
Class declarations are not hoisted (like function declarations)
You always need to declare your class before you use it.
List Of Objects
var list = [
{ name: 'David', id: 42 },
{ name: 'Steven', id: 65 }
];
Class Expression
These can be named or unnamed
let Something = class Something {
constructor(myheight, mywidth) {
this.height = myheight;
this.width = mywidth;
}
};
console.log(Something.name);
Constructor
This is a special method used for creating and initializing the object.
A constructor can use the "super" keyword to call the constructor of the super class.
this
You can use 'this' keyword to refer to the current object.
this' refers to the calling object in a method
function validate(obj, lower, upper) {
if ( (obj.value < lower) || (obj.value > upper) ) {
alert('out of range');
}
}
<input type="text" name="age" size="3"
onChange="validate(this, 18, 99)">
© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopNext