認識在js中的公有和私有變量
function car(){
var wheel = 3;//私有變量
this.wheel = 4;//公有變量
alert(wheel);
alert(this.wheel);
}
這里創建的都是私有變量 sum num1 num2
function sum(num1, num2) {
var sum = num1 + num2;
return sum;
}
我們怎么才能訪問到函數內部的私有變量,怎么才能修改函數內部的私有變量。
下面我簡單說下關于函數內部私有和公有變量的一些處理辦法。
如果在這個函數內部創建一個閉包,那么閉包通過自己的作用域鏈也可以訪問這些變量,利用這一點,就可以創建用于訪問私有變量的公用方法。
我們把有權訪問私有變量和私有函數的公有方法叫做特權方法。
- 1.構造函數定義法
function myObject() {
//私有屬性和私有函數
var privateVariable = 10;
function privateFunction() {
return privateVariable;
}
//特權方法
this.publicMethod = function() {
privateVariable++;
return privateFunction();
}
}
var object1 = new myObject();
//訪問了函數內部的局部變量即私有變量
console.log(object1.publicMethod());
function Person(name) {
this.getName = function() {
return name;
};
this.setName = function(value) {
name = value;
};
}
//創建對象實例
var person = new Person("李雷");
console.log(person.getName());
person.setName("韓梅梅");
console.log(person.getName());
在以上構造函數中定義了兩個特權方法:getName()和setName()。這兩個方法都可以在函數外部使用,而且都有權訪問私有變量name。
- 2.靜態私有變量
通過在私有作用域中定義私有變量或函數,同樣也可以創建特權方法
(function() {
//私有變量和私有函數
var privateVariable = 10;
function privareFunction() {
return false;
};
//構造函數
MyObject = function() {}
//公有、特權方法
MyObject.prototype.publicMethod = function() {
privateVariable++;
privareFunction();
}
})();
(function() {
var name = "";
Person = function(value) {
name = value;
};
//添加原型方法
Person.prototype.getName = function() {
return name;
};
Person.prototype.setName = function(value) {
name = value;
};
})();
var person1 = new Person("lilei");
console.log(person1.getName());
person1.setName("hanmeimei");
console.log(person1.getName());
var person2 = new Person("mahuateng");
console.log(person1.getName());
console.log(person2.getName());
- 3.模塊模式
模塊模式是為單例創建私有變量和特權方法,單例就是指,只有一個實例的對象。
var singlton = {
name: value,
method: function() {
//這是方法的代碼
}
}
var singlton = function() {
//私有變量和私有函數
var privateVaiable = 10;
function privateMethod() {
return false;
};
//公有/特權方法
return {
publicProperty: true,
piblicMethod: function() {
privateVaiable++;
return privateMethod();
}
}
}();
function BaseComponent() {}
function OtherComponent() {}
var application = function() {
//私有變量和函數
var components = new Array();
//初始化
components.push(new BaseComponent());
//公有
return {
getComponentCount: function() {
return components.length;
},
registerComponent: function(component) {
if(typeof component == "object") {
components.push(component);
}
}
};
}();
application.registerComponent(new OtherComponent());
alert(application.getComponentCount()); //2
- 4.增強的模塊模式
var singlton = function(){
//私有變量和私有函數
var privateVaiable = 10;
function privateMethod() {
return false;
};
//創建對象
var object = new CustomType();
//公有/特權方法
object.publicProperty = true;
object.piblicMethod = function() {
privateVaiable++;
return privateMethod();
};
//返回這個對象
return object;
}();
function BaseComponent() {}
function OtherComponent() {}
var application = function() {
//私有變量和私有函數
var components = new Array();
//創建對象
components.push(new BaseComponent());
//創建application的一個局部副本
var app = new BaseComponent();
//公共
app.getComponentCount = function() {
return components.length;
};
app.registerComponent = function(component) {
if(typeof component == "object") {
components.push(component);
}
};
//返回這個副本
return app;
}();
alert(application instanceof BaseComponent);
application.registerComponent(new OtherComponent());
alert(application.getComponentCount()); //2