Javascript is a C-like language. Its object-oriented features are strange compared to C++/Java, but it is indeed quite powerful. In the past two days, a former colleague has been asking me about object-oriented programming in Javascript, so I would like to write an article for him to read. This article mainly wants to explain object-oriented programming in Javascript from an overall perspective. Reposted from an article by Cool Shell, it is very well written...
In addition, this article is mainly based on ECMAScript 5 and aims to introduce new technologies. Regarding compatibility, please see the last section.
Preliminary exploration
We know that the definition of variables in Javascript is basically as follows:
var name = 'Chen Hao';;
var email = 'haoel(@)hotmail.com';
var website = 'http:/ /coolshell.cn';
If you want to write it in objects, it will look like this:
var chenhao = {
name :'Chen Hao',
email : 'haoel(@)hotmail.com',
website: 'http://coolshell.cn'
}
So, I can access like this:
//As a member
chenhao.name;
chenhao.email;
chenhao. website;
//In the form of hash map
chenhao["name"];
chenhao["email"];
chenhao["website"];
About functions, we know that Javascript functions are like this of:
var doSomething = function(){
alert('Hello World.');
};
So, we can do this:
var sayHello = function(){
var hello = " Hello, I'm “+ this.name
+ “, my email is: ” + this.email
+ “, my website is: ” + this.website;
alert(hello);
};
//Direct assignment, this is very similar to the function pointer of C/C++
chenhao.Hello = sayHello;
chenhao.Hello();
I believe these things are relatively simple and everyone understands it. You can see that JavaScript object functions are directly declared, assigned values, and used directly. Dynamic language for runtime.
There is also a more standardized way of writing:
//We can see that it uses function as a class.
var Person = function(name, email, website){
this.name = name;
this.email = email;
this.website = website;
this.sayHello = function(){
var hello = "Hello, I'm "+ this.name + ", n" +
"my email is: " + this.email + ", n" +
"my website is: " + this.website ;
alert(hello);
};
};
var chenhao = new Person("Chen Hao", "haoel@hotmail.com",
"http://coolshell.cn");
chenhao.sayHello();
By the way, to delete the attributes of an object, it is very simple:
delete chenhao['email']
From the above examples, we can see the following points:
◆ Javascript's data and member encapsulation is very simple. No class is entirely an object operation. Purely dynamic!
◆ The this pointer in Javascript function is critical. If not, it is a local variable or local function.
◆ Javascript object member functions can be temporarily declared when used, and a global function can be directly assigned to it.
◆ Javascript member functions can be modified on the instance, which means that the behavior of the same function name in different instances may not be the same.
Property configuration – Object.defineProperty
Look at the following code first:
//Create an object
var chenhao = Object.create(null);
//Set a property
Object.defineProperty( chenhao,
'name', { value: 'Chen Hao',
writable: true,
configurable: true,
enumerable: true });
//Set multiple properties
Object.defineProperties(chenhao,
{
'email' : { value: 'haoel@hotmail.com',
writable: true,
configurable: true,
enumerable: true },
'website': { value: 'http: //coolshell.cn',
writable: true,
configurable: true,
enumerable: true }
}
);
Let’s talk about what these attribute configurations mean.
writable: Whether the value of this attribute can be changed.
configurable: Whether the configuration of this attribute can be changed.
enumerable: Whether this property can be traversed in a for...in loop or enumerated in Object.keys.
value: attribute value.
get()/set(_value): get and set accessors.
Get/Set accessor
About the get/set accessor, it means to use get/set to replace value (it cannot be used with value). The example is as follows:
var age = 0;
Object. defineProperty( chenhao,
'age', {
get: function() {return age+1;},
set: function(value) {age = value;}
enumerable : true,
configurable : true
}
);
chenhao.age = 100; //Call set
alert(chenhao.age); //Call get and output 101 (+1 in get);
Let’s look at another update As a practical example - use the existing attribute (age) to construct a new attribute (birth_year) through get and set:
Object.defineProperty(chenhao,
'birth_year',
{
get: function() {
var d = new Date();
var y = d.getFullYear();
return ( y – this.age );
},
set: function(year) {
var d = new Date();
var y = d.getFullYear();
this.age = y – year;
}
}
);
alert(chenhao.birth_year);
chenhao.birth_year = 2000;
alert(chenhao.age);
It seems a bit troublesome to do this. You said, why don’t I write it like this:
var chenhao = {
name: "Chen Hao",
email: "haoel@hotmail.com",
website: "http://coolshell.cn",
age: 100,
get birth_year() {
var d = new Date();
var y = d.getFullYear();
return ( y – this.age );
},
set birth_year(year) {
var d = new Date( );
var y = d.getFullYear();
this.age = y – year;
}
};
alert(chenhao.birth_year);
chenhao.birth_year = 2000;
alert (chenhao.age);
Yes, you can indeed do this, but through defineProperty() you can do these things:
1) Set property configurations such as writable, configurable, enumerable, etc.
2) Dynamically add attributes to an object. For example: some HTML DOM objects.
View the object property configuration
If you view and manage these configurations of the object, there is a program below that can output the properties and configuration of the object:
// List the properties of the object.
function listProperties(obj)
{
var newLine = ”
“;
var names = Object.getOwnPropertyNames(obj);
for (var i = 0; i
var prop = names[i] ;
document.write(prop + newLine);
// List the object’s property configuration (descriptor) using the getOwnPropertyDescriptor function.
var descriptor = Object.getOwnPropertyDescriptor(obj, prop);
for (var attr in descriptor) {
document.write(“…” + attr + ': ' + descriptor[attr]);
document. write(newLine);
}
document.write(newLine);
}
}
listProperties(chenhao);
call, apply, bind and this
About the this pointer of Javascript, and C++/ Java is very similar. Let's take a look at an example: (This example is very simple, I won't say more)
function print(text){
document.write(this.value + ' – ' + text+ '
');
}
var a = {value: 10, print : print};
var b = {value: 20, print : print};
print('hello');// this => global, output " undefined – hello”
a.print('a');// this => a, output “10 – a”
b.print('b'); // this => b, output “ 20 – b”
a['print']('a'); // this => a, output “10 – a”
Let’s look at call and apply again. The difference between these two functions is the parameters The appearance is different, and the other is that the performance is different. The performance of apply is much worse. (For performance, you can go to JSPerf and take a look)
print.call(a, 'a'); // this => a, output “10 – a”
print.call(b, 'b '); // this => b, output “20 – b”
print.apply(a, ['a’]); // this => a, output “10 – a”
print. apply(b, ['b']); // this => b, output "20 – b"
But after bind, the this pointer may be different, but because Javascript is dynamic. As in the following example
var p = print.bind(a);
p('a'); // this => a, output “10 – a”
p.call(b, 'b' ); // this => a, output “10 – b”
p.apply(b, ['b’]); // this => a, output “10 – b”
Inheritance and Replication
Through the above examples, we can actually inherit through Object.create(). Please see the code below. Student inherits from Object.
var Person = Object.create(null);
Object.defineProperties
(
Person,
{
'name' : { value: 'Chen Hao'},
'email' : { value : 'haoel@hotmail .com'},
'website': { value: 'http://coolshell.cn'}
}
);
Person.sayHello = function () {
var hello = “
Hello, I am “+ this.name + “,
” +
“my email is: ” + this.email + “,
” +
“my website is: ” + this.website;
document. write(hello + “
”);
}
var Student = Object.create(Person);
Student.no = “1234567″; //Student number
Student.dept = “Computer Science”; / /Department
//Using the attributes of Person
document.write(Student.name + ' ' + Student.email + ' ' + Student.website +'
');
//Methods of using Person
Student.sayHello();
//Overload the SayHello method
Student.sayHello = function (person) {
var hello = “
Hello, I am “+ this.name + “,
” +
“my email is: ” + this.email + “,
” +
“my website is: ” + this.website + “,
” +
“my student no is: ” + this. no + “, < ;br>” +
“my departent is: ” + this. dept;
document.write(hello + '
');
}
//Call again
Student.sayHello();
// View the properties of Student (only no, dept and overloaded sayHello)
document.write('
' + Object.keys(Student) + '
');
Common to the above example, We can see that the attributes in Person are not actually copied to Student, but we can access them. This is because Javascript implements this mechanism using delegates. In fact, this is the prototype, and Person is the prototype of Student.
When our code needs an attribute, the Javascript engine will first check whether the current object has this attribute. If not, it will search whether its Prototype object has this attribute, and continue until it is found. Or until there are no Prototype objects.
To prove this, we can use Object.getPrototypeOf() to check:
Student.name = 'aaa';
//Output aaa
document.write('
' + Student.name + '
');//Output Chen Hao
document.write('
' +Object.getPrototypeOf(Student).name + '
');So, you You can also call the function of the parent object in the function of the child object, just like Base::func() in C++. Therefore, when we overload the hello method, we can use the code of the parent class, as shown below:
//New version of the overloaded SayHello method
Student.sayHello = function (person) {
Object.getPrototypeOf(this).sayHello .call(this);
var hello = “my student no is: ” + this. no + “,
” +
“my departent is: ” + this. dept;
document.write(hello + '
');
}
This is very powerful.
Combination
The above thing does not meet our requirements. We may hope that these objects can be truly combined. Why combination? Because we all know that this is the most important thing in OO design. However, this does not support Javascript particularly well, so we can still get it done.
First, we need to define a Composition function: (target is the object that acts on it, source is the source object). The following code is very simple. It just takes out the attributes in the source one by one and defines them in the target. On Fulling composition (target, source) {
var desc = object.get.getownpropertydescriptor; opt = object.defineProperty;
Prop (Source). Foreach (
function(key) {
def_prop(target, key, desc(source, key))
}
)
return target;
}
With this function, we can play here:
//Artist
var Artist = Object.create(null);Artist.sing = function() {
return this.name + ' starts singing…';}
Artist.paint = function() {return this.name + ' starts painting…';
}
//Athletes
var Sporter = Object.create(null);
Sporter.run = function() {
return this.name + ' starts running…';
}
Sporter.swim = function() {
return this.name + ' starts swimming…';
Composition(Person, Artist);
document.write(Person.sing() + '
' );
document.write(Person.paint() + '
');
Composition(Person, Sporter);
document.write(Person.run() + '
');
document.write(Person.swim() + '
');
//See what's in Person? (Output: sayHello,sing,paint,swim,run)
document.write('
' + Object.keys(Person) + '
');
Let’s do it first Let’s talk about Prototype. Let's take a look at the following routine first. This routine does not need explanation. It is very similar to the function pointer in C language. There are many such things in C language.
document.write( x + ' + ' + y + ' = ' + (x+y) + '
');
return x + y;
document.write(x + ' – ' + y + ' = ' + (x-y) + '
');
};
var operations = {
'+': plus,
};
var calculate = function(x, y, operation){
return operations[operation](x, y);
calculate(12, 4, '+');
calculate(24, 3, '-');
var Cal = function(x, y){
this.y = y;
}
Cal.prototype.operations = {
'+': function(x, y) { return x+y;},
'-': function(x, y) { return x-y;}
};
Cal.prototype.calculate = function(operation){
return this.operations[operation](this.x, this.y);
};
var c = new Cal(4, 5);
c.calculate('+');
c.calculate('-');
This is the usage of prototype. Prototype is the most important content in the JavaScript language. There are too many articles on the Internet about this thing. To put it bluntly, prototype is to extend an object, and its characteristic is to return a new instance by "copying" an existing instance instead of creating a new instance. The copied instance is what we call a "prototype", and this prototype is customizable (of course, there is no real copy here, it is actually just a delegation). In the above example, we extended the instance Cal to have an operations attribute and a calculate method.
In this way, we can implement inheritance through this feature. Remember our first Person, the following example is to create a Student to inherit Person.
function Person(name, email, website){
this.name = name;
this.email = email;
this.website = website;
};
Person.prototype.sayHello = function(){
var hello = “Hello, I am “+ this.name + “,
” +
“my email is: ” + this.email + “,
” +
“my website is: ” + this .website;
return hello;
};
function Student(name, email, website, no, dept){
var proto = Object.getPrototypeOf;
proto(Student.prototype).constructor.call(this, name, email, website);
this.no = no;
this.dept = dept;
}
//Inherit prototype
Student.prototype = Object.create(Person.prototype);
//Reset constructor
Student.prototype.constructor = Student;
//Overload sayHello()
Student.prototype.sayHello = function(){
var proto = Object.getPrototypeOf;
var hello = proto(Student.prototype).sayHello.call (this) + '
';
hello += “my student no is: ” + this. no + “,
” +
“my departent is: ” + this. dept;
return hello ;
};
var me = new Student(
"gtfx",
"gtfx0209@gmail.com",
"http://dyygtfx.com",
"12345678″,
"Computer Science"
);
document.write(me.sayHello());
Compatibility
The above codes may not be able to run in all browsers, because the above codes follow the specifications of ECMAScript 5. About ECMAScript 5 For the browser compatibility list, you can see the "ES5 Browser Compatibility List" here.
All the code in this article has been tested in the latest version of Chrome.
The following are some functions that can be used in browsers that are not ES5 compatible:
Object.create() function
function clone(proto) {
function Dummy() { }
Dummy.prototype = proto;
Dummy.prototype.constructor = Dummy;
return new Dummy(); //Equivalent to Object.create(Person);
}
var me = clone(Person);
defineProperty() function
function defineProperty(target, key, descriptor) {
if (descriptor.value){
target[key] = descriptor.value;
}else {
descriptor.get && target.__defineGetter__(key, descriptor. get);
descriptor.set && target.__defineSetter__(key, descriptor.set);
}
return target
}
keys() function
function keys(object) { var result, key
result = [];
for (key in object){
if (object.hasOwnProperty(key)) result.push(key)
}
return result;
}
Object.getPrototypeOf() function
function proto(object) {
return !object? null
: '__proto__' in object? object.__proto__
: /* not exposed? */ object.constructor.prototype
}
bind function
var slice = [].slice
function bind(fn, bound_this) { var bound_args
bound_args = slice.call(arguments, 2)
return function() { var args
args = bound_args.concat(slice .call(arguments))
return fn.apply(bound_this, args) }
}
The above is the content of the JavaScript object-oriented tutorial. For more related content, please pay attention to the PHP Chinese website (www.php.cn )!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.