Home > Web Front-end > JS Tutorial > body text

How to set members in nodejs

藏色散人
Release: 2021-10-28 14:13:03
Original
1730 people have browsed it

Nodejs method to set members: 1. Create a js sample file; 2. After the object is generated, attach member variables to it through "c.name="my circle"".

How to set members in nodejs

The operating environment of this article: Windows 7 system, nodejs version 10.16.2, DELL G3 computer

How to set members in nodejs?

node.js defines member variables:

Node.js is a JavaScript running environment based on the Chrome V8 engine. Node.js uses an event-driven, non-blocking I/O model.

node.js is JavaScript running on the server. Let’s take a look at how node.js defines member variables:

Member variables

Member variables are declared in the initialization function: this.r = r;

Note that after the object is generated, you can also attach member variables to it, such as c.name="my circle",

But unless there is a special need, I strongly recommend you not to do this. That is, all members should be declared in the initialization function. I think this is a good style.

p.c="ccc";
function p(){this.b="ccc"}
var d=new p();
var f=new p();
d.v=33;
alert(p.c);//ccc
alert(d.c);//undefined
alert(f.c);//undefined
alert(p.b);//undefined
alert(d.b);//ccc
alert(f.b);//ccc
alert(p.v);//undefined
alert(d.v);//33
alert(f.v);//undefined
Copy after login

Member function

The standard form of member function is this:

Cricle.prototype.area = function() { return 3.14 * this.r * this.r; }
Copy after login

This is very different from java or python or c. But to help understand, you can think of prototype as a base class.

The variables or methods in prototype are shared by all objects.

For example, the c.area() call will eventually cause the interpreter to call Circle.prototype.area().

Compared with java and c, javascript has a feature that neither of them has Semantics, that is, you can define variables in the prototype. Variables defined in the prototype can be shared by all instances. So generally it should be a constant, such as: Circle.prototype.PI = 3.14.

Obviously, the variables and methods in the prototype should be unchanged. Each object instance should not modify the contents of the prototype. Although the language allows you to do this, it makes no sense and violates object-oriented semantics.

It is recommended that all member functions be defined immediately next to the class definition. Instead of adding/modifying member functions to an object instance somewhere while the code is running. The result is that JavaScript's class definitions are as consistent as Java's. Makes the code clearer.

Recommended learning: "node.js Video Tutorial"

The above is the detailed content of How to set members in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template