您的位置:首页 > Web前端 > JavaScript

Js-----var x, this.x, x....inheritance

2010-01-27 19:05 423 查看
In JavaScript area,,,as far as I know, The JavaScript execution context is a concept that explains much of the behavior of JavaScript functions. The execution context represents the environment in which a piece of JavaScript code executes. JavaScript knows of three execution contexts:

The global execution context is the implicit environment (context) in which the JavaScript code that is not part of any function executes.

The function execution context is the context in which the code of a function executes. A function context is created automatically when a function is executed, and removed from the contexts stack afterwards.

The eval() execution context is the context in which JavaScript code executed using the eval( ) function runs.

The scope of the global execution context contains the locally defned variables and unctions, and the browser's window object. In that context, this is equivalent to window, so you can access, for example, the location property of that object using either this. location or window. location.

The scope of a function execution context contains the function's parameters, the locally defned variables and functions, and the variables and functions in the scope
of the calling code. This explains why the getCellCount( ) function has access to the _rows and _columns variables that are defned in the outer function (Table):

// Table class
function Table (rows, columns)
{
// save parameter values to local variables
var _rows = rows;
var _columns = columns;
// return the number of table cells
this. getCellCount = function()
{
return _rows * _columns;
} ;
}

The scope of the eval() execution context is identical to the scope of the calling code context. The getCellCount( ) function from the above code snippet could be written like this, without losing its functionality:
// return the number of table cells
this. getCellCount = function ()
{
return eval(_rows * _columns) ;
} ;

var x, this.x, and x:

Actually, I think object of JavaScript is similar with Association Array. we can declare a object as this:

代码

function Drive(){
alert('Drive in Car');
}
function car(name){
this.Name = name;
this.drive = Drive;
}
function BenCar(name){
this.inheritanceCar = car;
this.inheritanceCar(name);
this.fly=Fly;
}
function Fly(){
alert('Fly in BenCar')
}

var obj = new BenCar("Demo");
obj.drive();
obj.fly();

However, if we do as above shows. I think it will generate many functions in Global Execute Context. And it will result in Naming Collision. Thus we can use the Prototype feature of JavaScript to instead.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: