This document discusses object-oriented programming in JavaScript. It covers built-in objects like Object, Function, and Array. It also discusses creating custom objects using constructor functions and prototypes. Constructor functions allow initializing object state, while prototypes allow defining shared object behavior. JavaScript uses prototypal inheritance, where an object inherits directly from another object instance.
Overview of Object Oriented Programming (OOP) in JavaScript.
JavaScript objects consist of unordered collections of name-value pairs, where values can also be functions.
Discussion on built-in objects: Object, Function, Array, Date, RegExp, Error, Math, JSON, and Primitive wrapper objects.
Built-in objects can be created via constructors using new expressions or alternative syntax with object literals.
Custom objects can be created using functions or constructor functions, with properties and methods defined depending on prototype usage.
Constructor functions and prototypes define object state at instance level and behavior at prototype level, including property search in prototype chain.
Schematic representation of constructor prototypes.
Overview of combination inheritance in JavaScript, employing prototype chaining and constructor stealing.
Practical demonstration of JavaScript inheritance.
Prototypal inheritance allows a new instance to inherit directly from another without needing constructor functions.
Practical example illustrating prototypal inheritance in JavaScript.
JavaScript offers strong OOP features, differing from class-based languages, with multiple implementation options.
JavaScript object basics ● ● InJavaScript an object is an (unordered) collection of name-value pairs Please note that in JavaScript values can be functions!
Built-in object creation ● Objectsare created using constructors in new expressions – ● ● Alternative syntax is the object literal Objects without constructors can be used immediately. For instance: x = Math.PI; Wrapper objects are created automatically when needed JavaScript built-in objects fiddle
5.
Custom object creation Customobjects can be created using a ● ● … regular JavaScript function that returns an object reference … constructor function invoked in the context of a new expression Please note; – A constructor is a regular JavaScript function! – In case no prototype functionality is used all properties and methods will be created on the object instance JavaScript custom objects fiddle
6.
Constructor function &prototype ● Using both the constructor function and prototype it's possible to define – – ● ● object state at the instance level and object behavior at the level of the shared prototype When reading JavaScript properties the entire prototype chain is searched in case a property cannot be located on an object instance Individual object instances can override inherited behavior JavaScript constructor function and prototype fiddle schema on next slide!
JavaScript inheritance ● Combination inheritance – isthe most common way to implement inheritance in JavaScript – has two components ● ● Prototype chaining for the implementation of (shared) base class behavior Constructor stealing for the initialization of base class properties on derived object instances JavaScript combination inheritance fiddle schema on next slide!
Prototypal inheritance ● In prototypalinheritance – – ● a new object instance inherits directly from another object instance there is no need to implement constructor functions In ECMAScript (fifth edition) prototypal inheritance is implemented as follows: var die_3 = Object.create(die_1); JavaScript prototypal inheritance fiddle schema on next slide!
#5 //Declaration object references var obj_inst_1, obj_inst_2; //Use the Object constructor in conjunction with the new operator to //instantiate a new object and assign the reference to obj_inst_1 obj_inst_1 = new Object(); //In JavaScript object properties and methods can be added on the fly obj_inst_1.prop = "Added on the fly"; obj_inst_1.func = function() {return this.prop + "!";}; toFiddleResult(obj_inst_1.func()); //Object literal, an alternative way to instantiate Javascript objects obj_inst_2 = { prop: "Added as part of the object literal", func: function() {return this.prop + "!";} }; toFiddleResult(obj_inst_2.func()); //There is no need to instantiate objects without constructors, i.e. //the global object, Math & JSON var global = this; //In top level code this refers to the global object toFiddleResult("Is 42 a finite number? " + global.isFinite(42)); toFiddleResult("The number u03c0 = " + Math.PI); //Primitive wrapper objects are created automatically when needed var str = "u00a9 Hans de Rooij"; toFiddleResult(str.substring(2, 6) + " wrote this code"); //Function for displaying code results in the JSFiddle results pane function toFiddleResult(str) { //Reference to document node containing fiddle results var result_div = document.getElementById("fiddle_result"); //Fiddle results are listed in pre tags var add_pre = document.createElement("pre"); add_pre.appendChild(document.createTextNode(str)); result_div.appendChild(add_pre); }