_javascript technique of public variable and private variable in JS class
Source: Internet
Author: User
On the cnblogs to read some of the articles on JS, make a note:
First look at code 1:
function car () {
var wheel = 3;//Private variable
This.wheel = 4;//public variable
alert (wheel);
alert (This.wheel);
}
var car1 = new Car (); The result: 3 4
Code 2:
function car () {
var wheel = 3;//Private variable
This.wheel = 4;//public variable
}
var car1 = new car ();
alert (Car1.wheel); Results: 4
var wheel = 3 is a local variable, this.wheel=4 is a public variable, and if you want to access the private variable in car, see 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 ()); Results: 3
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.