Home > Web Front-end > JS Tutorial > Public variables and private variables in js classes_javascript skills

Public variables and private variables in js classes_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 19:02:25
Original
1038 people have browsed it

I read some articles about js on cnblogs and took notes:

Look at code 1 first:
function car(){
var wheel = 3;//private variable
this .wheel = 4;//Public variable
alert(wheel);
alert(this.wheel);
}
var car1 = new car(); The result is: 3 4

Code 2:
function car(){
var wheel = 3;//Private variable
this.wheel = 4;//Public variable
}
var car1 = new car();
alert(car1.wheel); Result: 4

var wheel = 3 is a local variable, this.wheel=4 is a public variable. If you want to access the private variables in car, please Look at code 3:
function car(){
var wheel = 3;//Private variable
this.wheel = 4;//Public variable
this.getPrivateVal = function(){
Return wheel;
}
}
var car1 = new car();
alert(car1.getPrivateVal()); Result: 3

Related labels:
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
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template