文章列表
作用域和执行上下文,很多人容易把它们混淆,在这简单地做个比较。
作用域是函数定义的时候就确定好的了,函数当中的变量是和函数所处的作用域有关,函数运行的作用域也是与该函数定义时的作用域有关。
上下文,主要是相对于关键字this的值来说的,this是由函数运行时决定的。打个比如,我们大家都知道“语境”这个词,语境指的是通过一定语言环境以揭示概念在相对关系下的意义的定义。通俗点就是一句在不同的场合表达的意思可能是不一样。比如,他傻了。一种情况是表示他真的变傻了,另一种是大家开玩笑的情况下对他的一个调侃。所以不同的场合表示的意义是不一样的。这个上下文跟语境类似。大家看下下面的例子:
function f ...
一个Ext继承模板:
MyComponent = Ext.extend
(
Ext.some
.component
, {
//缺省构造参数,可被自定义设置覆盖
propA: 1
,
initComponent: function
(
)
{
//在组件初始化期间调用的代码
//因为配置对象应用到了“this”,所以属性可以在这里被覆盖,或者添加新的属性
//(如items,tools,buttons)
Ext.apply
(
this
, ...
function Car(color){
this.color = color;
this.run = function(){document.write("车开了<br/>");};
}
function Bus(color, owner){
//获取指向Car的指针
this.newMethod = Car;
this.newMethod(color);
//删除指针
delete this.newMethod;
//定义新方法/属性
this.owner = function(){document.write ...
//取得Session
SessionFactory sessionFactory=getHibernateTemplate().getSessionFactory();
Session session=sessionFactory.openSession();
SQLQuery query=session.createSQLQuery(configMap.get(CommonUtil.CONFIG).toString());
//设置返回MAP格式
return query.setResultTransformer(Transformers.ALIAS_T ...
function Person(name, sex){
this.value = "123";
var value = "1234";
this.name = name;
this.sex = sex;
this.say = function(){document.write(this.name + " say hello!!")};
document.write(value);//"1234"
}
Person.prototype.cry = function(){
aler ...
function A(id){
this.id = id;
}
//A.prototype.said = function(){
// document.write(id);
//}
A.prototype = {
said : function(){
document.write(this.id);
}
}
function B(id){
B.superclass.constructor.call(this, id);
}
Ext.extend(B, A, {
//重写父类的said方法
said : function(){
doc ...