js常识

var a ='1&2';
alert(typeof a);//string
alert(a instanceof String);//false
alert(a.split('&')[0])//1
alert(a.split('&')[1])//2
alert(a.length);

var b = new String();
alert(typeof b);//object
alert(b instanceof String);//true

var c = '111';
alert(typeof Number(c));//强行转换
alert(typeof parseInt(c));

function A(){

}
A.c = 'aa';
A.prototype.color = "ss";

alert(A.hasOwnProperty("prototype"));//true

alert(A.hasOwnProperty("constructor"));//false

alert(A.hasOwnProperty("c"));//true

alert(A.constructor);//native code

var func=new function(){this.a="func"}
var myfunc=function(x){
var a="myfunc";
alert(this.a);
alert(x);
}
myfunc.call(func,"var");

call 就是劫持对象,说明白一点其实就是更改对象的内部指针,即改变对象的this指向的内容。

var func=new function(){this.a="func"}
这样写是完全正确的,等同于
function f(){this.a="func"}
var func=new f();
只是用了匿名函数而已

var a = function(){this.a = 'a'} alert(a.a)//错误

var a = new function(){this.a = 'a'}alert(a.a)//正确

var _st = window.setTimeout;
window.setTimeout = function(fRef, mDelay) {
if(typeof fRef == 'function'){
var argu = Array.prototype.slice.call(arguments,2);
var f = (function(){ fRef.apply(null, argu); });
return _st(f, mDelay);
}
return _st(fRef,mDelay);
}
function test(x){
alert(x);
}
window.setTimeout(test,1000,'fason');
</script>
call方法JScript参考中的说明:调用一个对象的一个方法,以另一个对象替换当前对象。call([thisObj[,arg1[, arg2[, [,.argN]]]]]),但是没有示例
apply方法JScript参考中的说明:应用某一对象的一个方法,用另一个对象替换当前对象。apply([thisObj[,argArray]])
实际上这两个的作用几乎是相同的,要注意的地方是call(thisObj[,arg1[, arg2[,)中的arg参数可以是变量,而apply([thisObj[,argArray]])中的参数为数组集合。
今早又看到一篇利用call实现继承的例子.呵呵..也一并贴出来.这个例子比较简单.就算是由浅入深吧
<script language="javascript" type="text/javascript">
function father(){//父类
var self=this; //私有变量,子类里不会继承!
var var_private="private variable"; //私有变量
this.var_public="public variable"; //公有变量

this.author="xling";
this.test=function(msg){ //公有方法
alert("该方法位于父类 :" + msg + "\n" + self.author);
}

var test2=function(){ //私有方法,子类不能调用
alert("这个方法是父类的私有方法");
}
}

function father2(){
this.email="xlingFairy#hotmail.com";
}

function suber(){//子类
father.call(this);//通过这一句来继承父类(father)类的可见变量及方法(this)
}

function sun(){
suber.call(this);
father2.call(this);//和上面的一句放在一起,實現多重繼承!爽啊!
}

var mySuber=new suber();
mySuber.test("参数是从子类的实例里传入的");
//mySuber.test2(); //这一句会发生错误码,因为test2是父类的私有类
alert("父类的私有变量,子类不能读取:" + mySuber.var_private);
alert("父类的公有变量,子类可以读取" + mySuber.var_public);

var mySun=new sun();
mySun.test("这个是从孙子级的实例里传入的参数");
alert("父类的私有变量,子类不能读取:" + mySun.var_private);
alert("父类的公有变量,子类可以读取" + mySun.var_public);
alert(mySun.email);

VAR 加不加还是有区别的 不加时全局变量 加有时候是局部变量

function aa(){
a = '11';
alert(a);
}

aa();
alert(a)//正常 因为是全局变量

function aa(){
var a = '11';
alert(a);
}

aa();
alert(a)//失败 因为是局部变量

Ext.DomHelper = function(){}();

已经是对象了 不需要NEW FUNCTION

var DomainList = new function() {}();

不是对象只是变量 需要NEW FUNCTION

var aa = new function(){
alert(11);
}()

aa = {}
aa = function(){
alert(11);
}();

var bb = {}等价于 var bb = new Object()

{}里不能定义VAR 变量

NEW FUNCTION(){}里可以

aa = new function(){

var c = 'cc';//私有变量

d = 'dd';//等于于这个对象完全无关 是全局变量 完全破坏了封装

this.d = 'dd'//共有变量

}

单例:

Foo = function(){

return {

init : function(){}

}

}();

需要new Foo

改成

Foo = (function(){

return {

init : function(){}

}

})();

就不需要new了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值