An object is a data type that consists of a collection of properties and methods.
Properties are values that are associated with an object, while methods are functions that are associated with an object.
Properties represent the characteristics of an object, such as its size, color, or name. Properties can be of different data types, including strings, numbers, and arrays.
Methods, on the other hand, represent the actions that can be performed on an object, such as changing its size or color. Methods are usually implemented as functions within the object, and they can also be used to retrieve information from the object or to manipulate its properties.
For example, consider an object representing a car. Properties of the car object could include its make, model, and color, while methods could include starting the engine, stopping the engine, and changing the speed.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
var person_1 = "Samantha Fox";
var person_2 = "John Smith";
document.write(person_1.length);
document.write("<br><br>");
document.write(person_2.length);
</script>
</body>
</html>
This script creates two variables (think about them as most basic custom object), person_1
and person_2
, which store strings representing people's names. It then uses the length
property of the string object to determine the number of characters in each name, and displays the results using the document.write()
method.
A string is a primitive data type that represents a sequence of characters. However, strings can also be objects, because there is a String object that provides additional properties and methods for working with strings.
When a string is created using the double quotes or single quotes, it is treated as a primitive data type.
This is just introductory way to think about objects in broad sense.
No comments:
Post a Comment