Tutorial 4JavaScript as OOP
Tutorial 4 JavaScript as OOP 1
JavaScriptandObjects·Technically,"Everything”in JavaScript is Object·Array,String,Number,..: An object contains properties and methods Property has a name and value.Methods are actions that can be performed on the object2
2 JavaScript and Objects • Technically, ”Everything” in JavaScript is Object • Array, String, Number, . • An object contains properties and methods • Property has a name and value • Methods are actions that can be performed on the object
SomeImportantJavaScriptKeywordsKeywordDescriptionStringTo manipulate text stringsNumberTomanipulatenumbersOperatorsToassignvalues,comparevalues,performarithmeticoperations...Statements"Instructions"tobe"executed"bythe web browserin HTMLMathTomanipulatecalculationsDateRetrievesandmanipulatesdatesandtimesArrayCreates new array objectsBooleanCreatesnewBooleanobjectsErrorReturnsrun-timeerrorinformationGlobalRepresents global variables and contains various built-inJS methodsConversionConvertsdifferentJSvaluestoNumber,String,Boolean3
3 Some Important JavaScript Keywords Keyword Description String To manipulate text strings Number To manipulate numbers Operators To assign values, compare values, perform arithmetic operations . Statements “Instructions" to be "executed" by the web browser in HTML Math To manipulate calculations Date Retrieves and manipulates dates and times Array Creates new array objects Boolean Creates new Boolean objects Error Returns run-time error information Global Represents global variables and contains various built-in JS methods Conversion Converts different JS values to Number, String, Boolean
String·Casesensitive? String Methods:. s.length;vars="WelcometoCUHK": s.indexOf("to") ;JavaScriptStringMethods: s.slice(3,7);15· s.replace("CUHK","HongKong");8come: s.toLowerCase();Welcometo HongKongwelcometocuhk· s.concat("","Campus");WelcometoCUHKCampuses.charAt(1);
4 String • Case sensitive • String Methods: • s.length; • s.indexOf(“to”) ; • s.slice(3,7); • s.replace(“CUHK”,”HongKong”); • s.toLowerCase(); • s.concat(" ", "Campus"); • s.charAt(1); . var s = “Welcome to CUHK
Number.Numberscanbewrittenwithscientificnotation.e.g.vary=123e-5;.NumberMethodsvar x=2.017;:Number.isFinite(x);JavaScriptNumberMethods:Number.islnteger(x);.Number.isNaN(x);true·x.toPrecision(3);falsefalse.x.toStringO);2.022.0175
5 Number • Numbers can be written with scientific notation • e.g. var y = 123e-5; • Number Methods • Number.isFinite(x) ; • Number.isInteger(x); • Number.isNaN(x); • x.toPrecision(3); • x.toString(); . var x = 2.017;