JavaScript私有成员
//创建一个新的用户对象,接受一个有许多属性的对象作为参数
function User(properties)
{
for(var i in properties){(function(which){
var p=i;
//创建此属性的一个新的读取器(getter)
which["get"+p] = function(){
return properties[p];
};
//创建此属性的一个新的设置器(setter)
which["set"+p] = function(val){
properties[p] = val;
};
})(this);
}
}
//创建一个新的用户对性实例,并把具有两个属性的一个对象传入作为种子
var user = new User({
name:"Bob";
age:44;
));
//注意name属性并不存在
//因为它是属性对象(properties object)的私有变量
alert(user.name == null);
//不过我们可以使用新的getname()方法来获得这个值
//因为此函数是动态生成的
alert(user.getname() =="Bob");
//最后,我们看到能够使用这个心生成的函数来设置或获得年龄
user.setage(22);
alert(user.getage()==22);
相关文档:
// 学习要想拷贝那么快就好了
//
// JavaScript 的继承是基于 prototype 的,每个对象的 prototype 是保存在对象的 __proto__ 属性中的,这个属性是内部(internal)的属性( 惯例是内部的或者隐藏的属性以 _ 开头)
// A prototype-based language has the notion of a prototypical object, an object used as a template ......
1. 函数在执行完 return 指令后就会停止执行代码.
function test(Num1,Num2){
return Num1+Num2;
alert(Num1+Num2); //never outputs
}
2. javascript 函数不能重载,也就是说可以在同一个作用域中定义多个同名函数,而最终执行的是后一个函数.
function test(num){
alert(num+10);
}
function ......
1. 某些基类如果不直接使用,而仅仅只是用于给子类提供通用的函数,这种情况下,基类被看作抽象类.
2. 在 javascript 的类中所有的方法和属性都是"公用的".
3. javascript 中的继承并不是明确规定的,而是通过模仿来实现的.有以下方法:
(1). 对象冒充
function A(sColor){
this.color = sColor;
this.showColo ......
with(document)
{
write ("test");
write
("dsasfda");
}
上面是用了with
如果不用的话就要这样写了
document.write (" ......
<script language="javascript" type="text/javascript">
var fullDate23=new Date();
var dateString22=new Date(fullDate23.getYear(),(fullDate23.getMonth()),fullDate23.getDate());
var dateString23=new Date(dateString22.valueOf()+1*24*60*60*1000);"&vbcrlf&_
var showDateString1=dateStri ......