Learn Javascript Note 1 Javascript Object 1
I have studied Js for a while before. Now I want to look at it again. It seems like new knowledge, indicating that I am not very good at Js (it is very bad, it has never been better, hey). Here, review
If you see it unfortunately, it's just your note.
The object-oriented idea in Js is absolutely very important. If you have read the code of great gods, you will know how important the object-oriented idea in Js is.
All things in JavaScript are objects: String, Number, Array, Date, and so on.
In JavaScript, objects are data with properties and methods.
Attributes and Methods
1. attributes are values related to objects. 2. The method is an action that can be executed on an object.
For example, cars are objects in real life.
Attributes of a car:
Car. name = Fiat car. model = 500 car. weight = 850 kg car. color = white
Car methods:
Car. start () car. drive () car. brake ()
In JavaScript, the object is data (variable) and has attributes and methods. When declaring a JavaScript variable:
var txt = new String("Hello World");Here, the String object has built-in properties and methods: txt. length (attribute) txt. indexOf ("World") (method)
Create a JavaScript Object (1) person = new Object ();
Person. firstname = "John ";
Person. lastname = "Doe ";
Person. age = 50;
Person. eyecolor = "blue"; (2)
Person = {firstname: "John", lastname: "Doe", age: 50, eyecolor: "blue "};
(3) Use the object constructor
function person(firstname,lastname,age,eyecolor){ this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; }