MST Mid-I
MST Mid-I
Mid-1
UNIT-1
1. Compare HTML and XHTML.
HTML (Hypertext Markup Language) and XHTML (Extensible Hypertext
Markup Language) are both markup languages used to create web pages, but they
have some key differences:
1. Syntax: HTML has a more lenient syntax compared to XHTML. HTML allows
for certain syntax errors, such as unclosed tags or attributes without values,
without causing issues in rendering. XHTML, on the other hand, follows stricter
XML rules, requiring all tags to be properly nested and closed.
2. XML Compliance: XHTML is a reformulation of HTML as an XML
application. This means that XHTML documents must conform to XML syntax
rules, such as having a root element and all tags being lowercase. HTML does not
require XML compliance.
3. DOCTYPE Declaration: In HTML, the DOCTYPE declaration is optional
and often omitted. In XHTML, it is mandatory and must be the first line of the
document. The DOCTYPE declaration specifies the version of XHTML being
used and helps browsers render the page correctly.
4. Case Sensitivity: XHTML is case-sensitive, while HTML is not. In XHTML,
all tag and attribute names must be written in lowercase.
5. Quotation Marks: XHTML requires attribute values to be enclosed in
quotation marks (either single or double quotes), while HTML does not strictly
enforce this rule.
6. Character Encoding: XHTML documents must specify a character encoding,
typically using the `<meta>` tag or the HTTP Content-Type header. HTML
documents can omit the character encoding declaration, but it's considered best
practice to include it.
7. Self-Closing Tags: In XHTML, empty elements must be self-closed with a
slash at the end of the tag (e.g., `<br />`). While HTML allows this syntax, it's not
required.
8. Parsing: XHTML is parsed as XML, which means that browsers will stop
rendering the page if they encounter syntax errors. HTML parsers, on the other
hand, are more forgiving and will try to render the page even if there are errors.
In summary, XHTML is a stricter and more standardized version of HTML,
designed to be compatible with XML. While XHTML has advantages in terms of
standardization and compatibility with XML tools, HTML is more forgiving and
easier to write for many developers. However, with the introduction of HTML5,
many of the distinctions between HTML and XHTML have become blurred, as
HTML5 allows for a mix of HTML and XML syntax.
OR
HTML vs XHTML
The following table highlights the major differences between HTML and
XHTML −
const person = {
firstName: "John",
lastName: "Doe",
age: 30
};
2. Constructor Function:
You can define a constructor function to create objects with similar properties
and behaviors. Constructor functions use the `new` keyword to instantiate new
objects.
function Person(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
const person1 = new Person("John", "Doe", 30);
const person2 = new Person("Jane", "Smith", 25);
3. Object.create():
You can use the `Object.create()` method to create a new object with a specified
prototype object.
const personProto = {
greeting: function() {
return `Hello, my name is ${this.firstName} ${this.lastName}.`;
}
};
const person = Object.create(personProto);
person.firstName = "John";
person.lastName = "Doe";
person.age = 30;
console.log(person.greeting()); // Output: Hello, my name is John Doe.
4. Class Syntax (ES6):
You can use the class syntax introduced in ECMAScript 2015 (ES6) to define
classes and create objects using the `new` keyword.
class Person {
constructor(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
greeting() {
return `Hello, my name is ${this.firstName} ${this.lastName}.`;
}
}
const person1 = new Person("John", "Doe", 30);
const person2 = new Person("Jane", "Smith", 25);
console.log(person1.greeting()); // Output: Hello, my name is John Doe.
console.log(person2.greeting()); // Output: Hello, my name is Jane Smith.
5. Factory Function:
You can create objects using factory functions, which are functions that return
new object instances.