1. Object-oriented programming
1. Process-oriented and object-oriented
1) Process-oriented: Focus on the process steps of how to solve a problem. The programming feature is to realize each step of the process step by function. There is no class and Object concept.
2) Object-oriented: Focus on which object solves the problem. The programming feature is that a class appears, the object is obtained from the class, and the object solves the specific problem.
For the caller, process-oriented requires the caller to implement various functions by himself. Object-oriented, only requires the caller to understand the function of the specific method in the object, and does not need to understand the implementation details in the method.
2. Three Object-Oriented Features
Three Object-Oriented Features Large feature inheritance, encapsulation, polymorphism. JS can simulate inheritance and encapsulation, but it cannot simulate polymorphism, so JS is an object-based language, not an object-oriented language in the traditional sense.
3. The relationship between classes and objects
1) Classes are abstract, objects are concrete (classes are the abstraction of objects, objects are the concretization of classes)
2) Classes are an abstract concept, only It can be said that classes have attributes and methods, but they cannot assign specific properties to attributes. For example, human beings have names, but they cannot say what their names are.
Second, Javascript object creationBuild method
1.var obj = {}//simple object plainObject object literal/object literal
2. Constructor (Features: big camel case naming rules)
1) The system’s own constructor
var obj = new Object();//equivalent to var object = {}
2) Custom function encapsulation
// Add parameters to the function to make the function self-defined. You can change the data of the corresponding parameters in the function at will. When producing objects, you must get new objects
function Car(color){ this.color=color; this.name = "BMW"; this.height = "1400"; this.lang = "4900"; this.weight = 1000; this. health = 100; this. run = function(){ this.health -- ; } }
var car = new Car(“red”);
3. The internal principle of the object constructor 3 steps
1) At the top of the function body Add an object implicitly: var this = {}
2) Execute the content in the function body
3) Return this object implicitly
3. Packaging
1. It is impossible for primitive values to have attributes and methods (undefined, null, number, boolean, string)
2. Some primitive values can be called after passing through the wrapper class
var num= 123;//is not an object
var num1=new Number(123);//is an object
3. The process of implicit wrapping class is as follows
var num = 4; num.len = 3;//Implicit conversion occurs, create a new digital object, then add attributes and assign values, and finally delete this object, so no error is reported during execution console.log(num.len);//Create a new digital object and add attributes, so the final output is undefined var str="abcd"; str. length=2; console.log(str.length); //The output is 4, and the output after the original value str wrapper class is new String('abcd').length, the string has its own length attribute, so the output length is 4
Recommended tutorial: “JS Tutorial”
The above is the detailed content of js object-oriented programming. For more, please pay attention to other related articles on 1024programmer.com!