Final Keyword

  • In PHP, Final keyword is applicable to only class and class methods. We cannot declare as Final in PHP.
  • So if we declare class method as a Final then that method cannot be override by the child class.
  • Same as method if we declare class as a Final then that class cannot be extended any more.

Example 1

<?php  
      
    class base  
    {  
        final public function dis1()  
        {  
            echo "Base class..";  
        }     
    }  
    class derived extends base  
    {  
        public function dis1()  
        {  
            echo "derived class";  
        }  
    }  
    $obj = new derived();  
    $obj->dis1();  
  
?>  

Output:

Final Keyword

Const keyword

  • Constants are one type of variable which we can define for any class with keyword const.
  • Value of these variables cannot be changed any how after assigning.
  • Class constants are different than normal variables, as we do not need $ to declare the class constants.
  • If we are inside the class then values of the constants can be get using self keyword, but accessing the value outside the class you have to use Scope Resolution Operator.

Example 1

<?php  
    //create class  
    class javatpoint  
    {  
        //create constant variable  
        const a= "This is const keyword example";  
    }  
//call constant variable.  
echo javatpoint::a;  
?>  

Output:

This is const keyword example

Example 2

<?php  
    //create class  
    class demo  
    {  
        //create constant variable  
        const a= 10;  
    }  
//call constant variable.  
echo demo::a;  
?>  

Output:

10

JavaScript User-defined Object Type

This tutorial covers the concept of the user-defined object type in JavaScript. We have already covered JavaScript object which is created using object initializer syntax or the Object constructor syntax, and instance of an Object type is created.

Just like the default Object type, using which we can create objects in JavaScript, we can define our custom type using which we can create custom objects in JavaScript.

Creating User-defined JavaScript Object Type

We can define a custom object type by writing a constructor function where the name of the function should start with an uppercase alphabet for example CarCupHuman, etc.

The constructor function is defined just as we define any other user-defined JavaScript Function.

Here is the syntax for defining a constructor function:

function Xyz(param1, param2, ... , paramN)
{
    this.param1 = param1;
    this.param2 = param2;
    ...
    this.paramN = paramN;    
}

Once we have the constructor function defined, we can use it to create an object using the new keyword as follows:

let customObj = new Xyz(paramValue1, paramValue2, paramValue3, ... , paramValueN);

Using this Keyword

In JavaScript we use this keyword to reference the current object, hence in the constructor function we create new properties (param1param2, …) for the object type and we set the values for the parameters provided at the time of object creation using the new keyword.

Few Points to Remember:

  1. We can update the constructor function as many times we want, adding more properties or removing properties from it.
  2. We can also store another object in an object’s property while creating the object uwing the new keyword.
  3. We can use default parameters to specify default values and Rest parameters while defining a user-defined object type’s constructor function.

Now let’s take a few examples to see how we can define a custom object type and create an object using the new keyword.

User-defined JavaScript Object Type Example

In the below code example we have created a new constructor function for object type Bike, and then created an object using the new keyword.

<!-- wp:paragraph -->
<p>This tutorial covers the concept of the user-defined object type in JavaScript. We have already covered&nbsp;JavaScript object&nbsp;which is created using&nbsp;<strong>object initializer syntax</strong>&nbsp;or the&nbsp;<strong>Object constructor</strong>&nbsp;syntax, and instance of an&nbsp;<code>Object</code>&nbsp;type is created.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Just like the default Object type, using which we can create objects in JavaScript, we can define our custom type using which we can create custom objects in JavaScript.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Creating User-defined JavaScript Object Type</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>We can define a custom object type by writing a constructor function where the name of the function should start with an uppercase alphabet for example&nbsp;<em>Car</em>,&nbsp;<em>Cup</em>,&nbsp;<em>Human</em>, etc.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The constructor function is defined just as we define any other user-defined&nbsp;JavaScript Function.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here is the&nbsp;<strong>syntax for defining a constructor function</strong>:</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">function Xyz(param1, param2, ... , paramN)
{
    this.param1 = param1;
    this.param2 = param2;
    ...
    this.paramN = paramN;    
}</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p>Once we have the constructor function defined, we can use it to create an object using the&nbsp;<code>new</code>&nbsp;keyword as follows:</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">let customObj = new Xyz(paramValue1, paramValue2, paramValue3, ... , paramValueN);</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>Using&nbsp;<code>this</code>&nbsp;Keyword</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In JavaScript we use&nbsp;<code>this</code>&nbsp;keyword to reference the current object, hence in the constructor function we create new properties (<em>param1</em>,&nbsp;<em>param2</em>, ...) for the object type and we set the values for the parameters provided at the time of object creation using the&nbsp;<code>new</code>&nbsp;keyword.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Few Points to Remember:</h3>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><li>We can update the constructor function as many times we want, adding more properties or removing properties from it.</li><li>We can also store another object in an object's property while creating the object uwing the&nbsp;<code>new</code>&nbsp;keyword.</li><li>We can use&nbsp;default parameters&nbsp;to specify default values and&nbsp;Rest parameters&nbsp;while defining a user-defined object type's constructor function.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Now let's take a few examples to see how we can define a custom object type and create an object using the&nbsp;<code>new</code>&nbsp;keyword.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>User-defined JavaScript Object Type Example</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In the below code example we have created a new constructor function for object type&nbsp;<strong>Bike</strong>, and then created an object using the&nbsp;<code>new</code>&nbsp;keyword.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We can create as many objects of type Bike once we have defined the constructor function for it. We can set different property values while creating objects. For example,</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">let myBike = new Bike("KTM", "Duke", 2020);
let yourBike = new Bike("Royal Enfield", "Classis", 2020);</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p>We can compare these objects using JavaScript operators and perform a lot of other operations too.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Using a user-defined Object as Property of another Object</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Let's define one more object type and then use it as a value in our&nbsp;<strong>Bike</strong>&nbsp;object type.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We have defined a new object type&nbsp;<strong>Person</strong>&nbsp;below, and have also added a new property&nbsp;<strong>owner</strong>&nbsp;in the&nbsp;<strong>Bike</strong>&nbsp;object type in which we will store the&nbsp;<strong>Person</strong>&nbsp;objects.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We can do anything with user-defined object types in JavaScript.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Using the&nbsp;<code>Object.create</code>&nbsp;method</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you do not want to get into the trouble of defining a constructor method to define a user-defined object type, you can use the Object.create() method to create an object of any prototype object which is already defined.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's take an example to understand this.</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">let Bike = {
    company: 'KTM',  // Default value of properties
    model: 'Duke 390',
    show: function() {  // Method to display property type of Bike
        document.write(this.company+" "+this.model);
    }
};

// using Object.create to create a new object
let newBike = Object.create(Bike);
newBike.company = 'Yamaha';
newBike.model = 'R15';
newBike.show();</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p>Yamaha R15</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As you can see in the code above, we can use the&nbsp;<code>Object.create</code>&nbsp;method to create a new object using an already defined object, and then if required we can change the properties values and use the object function too.</p>
<!-- /wp:paragraph -->

<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button -->
<div class="wp-block-button"><a class="wp-block-button__link" href="https://wp.me/pc22El-Vx">< prev</a></div>
<!-- /wp:button -->

<!-- wp:button -->
<div class="wp-block-button"><a class="wp-block-button__link" href="https://wp.me/pc22El-VP">next ></a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->

We can create as many objects of type Bike once we have defined the constructor function for it. We can set different property values while creating objects. For example,

let myBike = new Bike("KTM", "Duke", 2020);
let yourBike = new Bike("Royal Enfield", "Classis", 2020);

We can compare these objects using JavaScript operators and perform a lot of other operations too.

Using a user-defined Object as Property of another Object

Let’s define one more object type and then use it as a value in our Bike object type.

<!doctype html>
	<head>
		<title>Object in Object Example JavaScript</title>
	</head>
	<body>
		<script>
			/* defining a new constructor function */
			function Person(name, age, city) {
                this.name = name;
                this.age = age;
                this.city = city;
            }
            
            function Bike(company, model, year, owner) {
                this.company = company;
                this.model = model;
                this.year = year;
                this.owner = owner;
            }
            
            /* creating the object */
            let john = new Person("John Wick", 30, "New York");
            let myBike = new Bike("KTM", "Duke", 2010, john);
            
            document.write(myBike.company+"<br/>"+myBike.model+"<br/>"+myBike.year+"<br/>"+myBike.owner.name);
		</script>
	</body>
</html>

We have defined a new object type Person below, and have also added a new property owner in the Bike object type in which we will store the Person objects.

We can do anything with user-defined object types in JavaScript.

Using the Object.create method

If you do not want to get into the trouble of defining a constructor method to define a user-defined object type, you can use the Object.create() method to create an object of any prototype object which is already defined.

Let’s take an example to understand this.

let Bike = {
    company: 'KTM',  // Default value of properties
    model: 'Duke 390',
    show: function() {  // Method to display property type of Bike
        document.write(this.company+" "+this.model);
    }
};

// using Object.create to create a new object
let newBike = Object.create(Bike);
newBike.company = 'Yamaha';
newBike.model = 'R15';
newBike.show();

Yamaha R15

As you can see in the code above, we can use the Object.create method to create a new object using an already defined object, and then if required we can change the properties values and use the object function too.

JavaScript Literals and Keywords

JavaScript Literals are the fixed value that cannot be changed, you do not need to specify any type of keyword to write literals. Literals are often used to initialize variables in programming, names of variables are string literals.

A JavaScript Literal can be a numeric, string, floating-point value, a boolean value or even an object. In simple words, any value is literal, if you write a string “Studytonight” is a literal, any number like 7007 is a literal, etc.

JavaScript supports various types of literals which are listed below:

  • Numeric Literal
  • Floating-Point Literal
  • Boolean Literal
  • String Literal
  • Array Literal
  • Regular Expression Literal
  • Object Literal

JavaScript Numeric Literal

  • It can be expressed in the decimal(base 10), hexadecimal(base 16) or octal(base 8) format.
  • Decimal numeric literals consist of a sequence of digits (0-9) without a leading 0(zero).
  • Hexadecimal numeric literals include digits(0-9), letters (a-f) or (A-F).
  • Octal numeric literals include digits (0-7). A leading 0(zero) in a numeric literal indicates octal format.

JavaScript Numeric Literals Example

120 // decimal literal

021434 // octal literal

0x4567 // hexadecimal literal

JavaScript Floating-Point Literal

  • It contains a decimal point(.)
  • A fraction is a floating-point literal
  • It may contain an Exponent.

JavaScript Floating-Point Literal Example

6.99689 // floating-point literal

-167.39894 // negative floating-point literal

JavaScript Boolean Literal

Boolean literal supports two values only either true or false.

JavaScript Boolean Literal Example

true // Boolean literal

false // Boolean literal

JavaScript String Literal

A string literal is a combination of zero or more characters enclosed within a single(') or double quotation marks (").

JavaScript String Literal Example

"Study" // String literal

'tonight' // String literal 

String literals can have some special characters too which are tabled below.

String Special Characters:

CharacterDescription
\bIt represents a backspace.
\fIt represents a Form Feed.
\nIt represents a new line.
\rIt represents a carriage return.
\tIt represents a tab.
\vIt represents a vertical tab.
\'It represents an apostrophe or single quote.
\"It represents a double quote.
\\It represents a backslash character.
\uXXXXIt represents a Unicode character specified by a four-digit hexadecimal number.

JavaScript Array Literal

  • An array literal is a list of zero or more expressions representing array elements that are enclosed in a square bracket([]).
  • Whenever you create an array using an array literal, it is initialized with the elements specified in the square bracket.

JavaScript Array Literal Example

var emp = ["aman","anu","charita"];  // Array literal

JavaScript Regular Expression Literal

Regular Expression is a pattern, used to match a character or string in some text. It is created by enclosing the regular expression string between forward slashes.

JavaScript Regular Expression Example

var myregexp = /ab+c/; // Regular Expression literal

var myregexp = new RegExp("abc"); // Regular Expression literal

JavaScript Object Literal

It is a collection of key-value pairs enclosed in curly braces({}). The key-value pair is separated by a comma.

JavaScript Object Literal Example

var games = {cricket :11, chess :2, carom: 4}  // Object literal

JavaScript Keywords

Every programming language has its own keywords and reserved words. Every keyword is created to perform a specific task and the compiler or interpreter already knows about the built-in keywords and reserved words. JavaScript supports a rich set of keywords that are listed in the below table.

KeywordDescription
forThe for keyword is used to create a for-loop.
do/whileThe do and while both keywords are used to create loops in JavaScript.
if/elseThe if and else keywords are used to create conditional statements.
continueThe continue keyword is used to resume the loop.
breakIt is used to break the loop.
functionThe function keyword is used to declare a function.
debuggerIt is used to call the debugger function
classThe class keyword is used to declare the class.
returnReturn keyword is used to return function from the function.
floatIt is used to declare float type variable.
intIt is used to declare int type variable.
privateIt is an access modifier.
publicPublic is an access modifier that gives the class public access.
varVar is used to declare a variable.
switchThe switch creates various statement blocks and executes only on block depending on the condition or the case.
try/catchIt creates a block for error handling of the statements.

NOTE: While defining name of any function or variable, you should not use any keyword or reserved words.

In this tutorial, we learned about JavaScript literals, different types of literals along with examples. We also covered the JavaScript keywords and reserved words.