"use strict";
function myFunction() {
return this;
}
// returns undefined because we have used strict which doesn't allow default mode<button onclick="this.style.display='none'">
Click to Remove Me!
</button>
var person = {
firstName : "John",
lastName : "Doe",
id : 5566,
myFunction : function() {
return this; // [object Object]
return this.firstName + " " + this.lastName; // John Doe
}
};
var person1 = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}
var person2 = {
firstName:"John",
lastName: "Doe",
}
person1.fullName.call(person2); // Will return "John Doe"
person1.fullName.apply(person2); // Will return "John Doe"
function increase() {
var counter = 10;
}
// cannot access the counter variable here
In this example, the counter
variable is local to the increase() function. It cannot be accessible outside of the function.for (var i = 0; i < 5; i++) {
console.log(`Inside the loop: ${i}`);
}
console.log(`Outside the loop: ${i}`);
output:
Inside the loop: 0
Inside the loop: 1
Inside the loop: 2
Inside the loop: 3
Inside the loop: 4
Outside the loop: 5
In this example, the i
variable is a global variable. Therefore, it can be accessed from both inside and after the for loop.$schema
- The $schema keyword states that this schema is written according to the draft v4 specification
title
- the title of the schema
description
- the description of the schema
type
- defines the type of the JSON (object / array)
properties
- defines various keys and their value types, minimum and maximum values to be used in JSON file
required
- this keeps a list of required properties
minimum
- minimum acceptable value for the item
exclusiveMinimum
- If "exclusiveMinimum" is present and has boolean value true, the value must be greater than the value of "minimum"
maximum
- maximum acceptable value for the item
exclusiveMaximum
- If "exclusiveMaximum" is present and has boolean value true, the value must be lower than the value of "maximum"
multipleOf
- a numeric instance is valid against "multipleOf" if the result of the division of the instance by this keyword's value is an integer
maxLength
- the length of a string instance is defined as the maximum number of its characters
minLength
- the length of a string instance is defined as the minimum number of its characters
pattern
- a string instance is considered valid if the regular expression matches the instance successfully
...
JSON schema example{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
"id": {
"description": "The unique identifier for a product",
"type": "integer"
},
"name": {
"description": "Name of the product",
"type": "string",
"minLength": 5,
"maxLength": 20
},
"price": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
}
},
"required": [
"id",
"name",
"price"
]
}
JSON that the schema above belongs to:{
"id": 2,
"name": "Lorem ipsum dolor si",
"price": 0.5
}
If the JSON was:{
"id": 2.5,
"name": "Lorem ipsum dolor sit amet",
"price": -0.5
}
It would have the following errors:- Invalid type. Expected Integer but got Number
- String 'Lorem ipsum dolor sit amet' exceeds maximum length of 20
- Float -0.5 is less than minimum value of 0
For validating the JSON with its schema, the resource below can be usedhttps://www.jsonschemavalidator.net/
Dynamically
changes HTML table content using XMLHttpRequest after choosing any option on <select>
obj = { table: "customers", limit: 20 };
dbParam = JSON.stringify(obj);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
txt += "<table border='1'>"
for (x in myObj) {
txt += "<tr><td>" + myObj[x].name + "</td></tr>";
}
txt += "</table>"
document.getElementById("demo").innerHTML = txt;
}
}
xmlhttp.open("POST", "json_demo_db_post.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);
XML
stands for Extensible Markup Language
application/xml
is the official Internet media type for XML.
.xml
is the XML filename extension.
XML is extensible
− XML allows us to create our own self-descriptive tags.
XML carries the data, does not present it
− XML allows us to store the data irrespective of how it will be presented.
XML is a public standard
- XML was developed by an organization called the World Wide Web Consortium.
Any type of data can be expressed as an XML document
XML can be used to exchange
the information between companies and systems<?xml version="1.0" encoding = "UTF-8"?>
<student>
<name>George</name>
<city>Tbilisi</city>
<phone>(011) 123-4567</phone>
</student>
The names of XML-elements are enclosed by triangular brackets < >
<element>
Each XML-element needs to be closed
either with start or with end elements as shown below<element>...</element>
XML allows self-closing elements, for example if the tag empty<?xml version="1.0"?>
<student>
<name>George</name>
<city>Tbilisi</city>
<address/>
<phone>(011) 123-4567</phone>
</student>
Children elements must not overlap parent elements. i.e., an element end tag must follow all of its children's end tags.
<company>
is closed after the </contact-info>
tag but it's opened after </contact-info>
tag, which is wrong!<?xml version = "1.0"?>
<contact-info>
<company>Learn Practice Teach
</contact-info>
</company>
The following example shows the correct nested tags<?xml version = "1.0"?>
<contact-info>
<company>Applications.ge</company>
<contact-info>
One root element
is necessary for an XML document. In this example below, both <x>
and <y>
elements are at the top level and they don't have one parent element, which is wrong:<?xml version = "1.0"?>
<x>...</x>
<y>...</y>
XML-elements are case-sensitive
, which means that the start and the end elements names need to be exactly in the same case. This example is not correct XML document, because case sensitivity
is not correctly applied<?xml version="1.0"?>
<student>
<name>George</name>
<city>Tbilisi</city>
<address/>
<phone>(011) 123-4567</Phone>
</student>
In this case <phone>
and its close tag </phone>
is not in the same case