1. The object
Objects are the basis of JavaScript. Most of the functionality of JavaScript is object-based. At the most basic level, an object is a collection of properties.
Creates a new object that is stored in the ' obj ' variable
var obj = new Object ();
Set a property for this object
Obj.val = 5;
Obj.click = function () {
Alert ("Hello");
};
In shorthand, a key-value pair (Key/value pair) defines a property
var obj = {
Set the property name and property value by using the key-value pair (Key/value pairs)
Val:5,
Click:function () {
Alert ("Hello");
}
};
2. Creation of objects
Unlike most other object-oriented languages, JavaScript does not have classes (class) concepts. Most other object-oriented languages need to instantiate a specific class. But unlike JavaScript, in JavaScript the object itself can be used to create new objects, and objects can be inherited from other objects. This concept is called archetypal Inheritance (Prototypal inheritance).
Regardless of what object scenarios JavaScript uses, there should be a way to create new objects first. The practice of JavaScript is that any function can be instantiated as an object.
A simple function that accepts a name and deposits it in the current context
function User (name) {
THIS.name = name;
}
Specify a name to create a new object for the function
var me = new User ("My Name");
Set its Name property
Alert (Me.Name = = "my Name");
An instance of the user object
Alert (Me.constructor = = User);
Since user is just a function, what if it's used as a function?
User ("Test");
This point to the default Global Window object
Alert (Window.name = = "Test");
The constructor property exists in each object and always points to the function that created it. This allows you to effectively copy objects, create objects with the same base class, and assign different properties.
Create a new simple user object
function User () {}
Create a User Object
var me = new User ();
Use the constructor of the previous object to create an object
var you = new Me.constructor ();
The constructor of these two objects is essentially consistent
Alert (Me.constructor = = You.constructor);
3. Public methods are always accessible to end users in the context of an object. To implement this public method that can be used in each instance of an object. You must understand the properties of the prototype (prototype), which contains an object that can act as a base reference (base reference) for all new replicas. Essentially, the properties of all object prototypes can be found in each instance of the object.
Because the object's prototype is still an object, as with any other object, you can add new properties to it. The result of adding attributes to a stereotype is that each object instantiated by that prototype gets these properties, and the attributes are publicly owned.
Create a new user constructor
function User (name, age) {
THIS.name = name;
This.age = age;
}
Adds a new function to the object's prototype object
User.prototype.getName = function () {
return this.name;
};
Add a function to the prototype object again
User.prototype.getAge = function () {
return this.age;
};
Instantiate a new User object
var user = new User ("Bob", 44);
Both of the properties you added are in the object that you just created and have the appropriate context
Alert (user.getname () = = "Bob");
Alert (user.getage () = = 44);
4. Private method
Private methods and private variables allow only access by other private methods, private variables, and privileged methods. This method can define code that is accessible only to internal objects, but not to external access.
Represents an object constructor for a classroom
function Classroom (students, teacher) {
Private method for displaying all students in a class
Function disp () {
Alert (This.names.join (","));
}
Storing class data in public properties
This.students = students;
This.teacher = teacher;
Calling private methods to display errors
Disp ();
}
Create a new Classroom object
var class = new Classroom (["John", "Bob"], "Mr Smith");
Calling the Disp method fails because it is not a public method of the object
Class.disp ();