日期:2014-05-16 浏览次数:20516 次
一些基础概念:
#1 JavaScript 对象其实就是属性的集合。属性由键值对组成。通过[属性名]这种形式则总是可以保证正确性。
#2 函数:?函数本身也是对象。
function func(id){};
相当于:
var func = function(id){};
#3?this?表示当前上下文,即调用者的引用。
#4 词法作用域?即是在定义的时候就确定下来了。
#5 scope?在执行一个函数时,函数的参数和其局部变量会作为调用对象的属性进行存储
#6 call & apply
#7 ?由于构造函数定义了一个对象的类,所以属性constructor在有助于确定给定对象的类型.如,可以使用如下代码来确定一个未知对象的类型:if ((typeof o == "object") && (o.constructor == Date)),也可以用instanceof运算符:if ((typeof o == "object") && (o instanceof Date))
#8?对象的引用机制:对象不会被复制,只能通过引用来传递。
var recordDefParams = [];
recordDefParams[0] = {name:'id',type:'date'};
recordDefParams[1] = {name:'version'};
recordDefParams[recordDefParams.length] = {name:'deleted'};
function func(recordDefParams){
for (var i = 0, nLen = recordDefParams.length; i < nLen; i++) {
var obj = recordDefParams[i];
if (obj.type == 'date' && !obj.dateFormat) {
obj.dateFormat = 'xxx-xx-x';
}
}
return recordDefParams;
}
console.dir(recordDefParams);
console.log('===========================');
console.dir(func(recordDefParams));
?
#9
?
?
function addr(street, xno){
console.log(this);
this.street = street || 'beijinglu';
this.xno = xno || '1 hao';
this.toString = function(){
return 'Street:' + this.street + ' xno:' + this.xno;
};
};
var core1 = new addr();
console.log(core1.toString());
var core2 = new addr('北京路','一号');
console.log(core2.toString());
?
?
//动态构建新的匿名对象
function point(x,y){
this.x = x;
this.y = y;
return {'x':x,'y':y};
}
var a = point(3,4);
console.dir(a);
for(var key in a ){
console.log(key);
console.log(a[key]);
}
?
?
After read the blog of "JavaScript Core chapter 10 .writer the coding below for testing.
<html>
<head>
<title>
Js core
</title>
<script type="text/javascript">
//原型链
var base = {
name : 'Mark',
getName : function(){
return this.name;
}
};
var ext1 ={
id :'0',
__proto__ :base
};
var ext2 ={
id : '9',
__proto__ : base
};
//console.log(ext1.getName());
//构造器
function Task(id){
this.id = id;
};
Task.prototype.status = 'defalt_begin';
Task.prototype.execute = function(args){
return 'execute task_[' + this.id + ']' + this.status + ':' + args;
};
var task1 = new Task(1);
var task2 = new Task(2);
task1.status = 'activing';
task2.status = 'end...';
//console.log(task1.execute('task_1'));
//console.log(task2.execute('task_2'));
//this understander
var global = this;
var tom = {
name : 'tom',
id : '1',
getInfo: function(){
console.log(this.name + ':' + this.id);
}
}
var jetty = {
name : 'jetty',
getInfo : tom.getInfo
//等同于
//getInfo:function (){ console.log(this.name + ':' + this.id);} 在跑构造的时