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

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:

代码
//  create Table obj ect
var  t  =  
  { rows : 
3 ,
    columns : 
5 ,
    getCellCount : 
function  () {  return   this . rows  *   this . columns; }
  } ;
//  display obj ect field values
document. writeln( "  Your table has  "   +  t. rows  +   "  rows "   +
                 
"  and  "   +  t. columns  +   "  columns<br /> " ) ;
//  call obj ect function 
document. writeln( "  The table has  "   +  t. getCellCount()  +  
                 
"  cells< br /> "  ) ;

 

An execution context contains a collection of (key, value) associations representing(key, value) associations representing associations representing the local variables and functions, a prototype whose members can be accessedprototype whose members can be accessed through the this keyword, a collection of function parameters (if the context was created for a function call), and information about the context of the calling code. Members accessed through this, and those declared using var, are stored in separate places, except in the case of the global execution context where variables and properties are the same thing. In objects, variables declared through var are not accessible through function instances, which makes them perfect for implementing private "class" members. On the other hand, members accessed through this are accessible through function instances, so we can use them to implement public members.

When a member is read using its literal name, its value is frst searched for in the list of local variables. If it's not found there, it'll be searched for in the prototype.
To understand the implications, see the following function, which defnes a local variable x, and a property named x. If you execute the function, you'll see that the
value of x is read from the local variable, even though you also have a property with the same name:

function  BigTest()
{
  
var  x  =   1 ;
  
this . x  =   2 ;
  
  document. write(x) ; 
//  displays "1"
  document. write( this . x) ;  //  displays "2"
}

 

Calling this function, either directly or by creating an instance of it, will display 1 and 2—demonstrating that variables and properties are stored separately. Should you
execute the same code in the global context (without a function), for which variables and properties are the same, you'd get the same value displayed twice.
When reading a member using its name literally (without this), if there's no local variable with that name, the value from the prototype (property) will be read instead, as this example demonstrates:

 

function  BigTest()
{
  
this . x  =   2 ;
  document. write(x) ; 
//  displays "2"
}

 

 

Thus, we should use the right Execute Context so that it avoid memory leak or any other situation. Keep in mind with following points:

1. When an object is created from a function, this refers to that object.

2. In the case of a simple function call, no matter if the function is defned directly in the global context or in another function or object, this refers to the global context.

The second point is particularly important. Using this in a function that is meant to be called directly, rather than instantiated as an object, is a bad programming
practice, because you end up altering the global object. Take this example that demonstrates how you can overwrite a global variable from within a function:

=   0  ;
function  BigTest()
{
  
this . x  =   1 //  modify a variable of the global context
}
BigTest( ) ;
document. write( x) ; 
//  displays "1"

 

Modifying the global object can be used to implement various coding architectures or features, but abusing of this technique can be dangerous. On the other hand,
if BigTest is instantiated using the new keyword, the this keyword will refer to the new object, rather than the global object. Modifying the previous example as highlighted below, we can see the x variable of the global context remains untouched:

=   0  ;
function  BigTest()
{
  
this . x  =   1 //  create an internal obj ect property
}
var  obj  =   new  BigTest() ;
document. write( x) ; 
//  displays "0"

 

But I think the above solutino is not legant and ideal. See the following:

=   0  ;
function  BigTest()
{
  
if  ( !  ( this   instanceof  BigTest) )  return   new  BigTest() ;
  
this . x  =   1 ;
}
BigTest( ) ;
document. write( x) ; 
//  displays "0"

 

 

JavaScript: Inheritance with Closure and Prototype.

代码
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.

 

你可能感兴趣的:(inheritance)