JS function, object, instance method, object method understanding


var _s = function(){

var name = 'lisa'; //internal property
var age = 10;
var sen = function () {
console.log("function_s") ; //Internal properties of the function, the scope is only inside the function,
}
return "1";
}
function _e(){
name = 'lisa';
age = 10;
sen = function(){
console.log("Function_E");
}
}
var Person = function () {
this.sn = 'sam'; //instance properties, instance methods
};
Person.say=function(){ //static method
    console.log('I am a Person,I can say.')
};
var person1 = new Person();
person1.getName=function(name){ //instance method
    console.log('My name is '+name);
}
Person.prototype.gets = function(){ // instance method, function prototype property A defined property or function can be shared by all instance objects of the function.
console.log("prototype method");
}
console.log(window.sn); //In a normal function this points to the current window object, so the assignment is also
//After the new function object is instantiated, the this point of the function property changes, this changes from the original point to the window object to the current instance object
console.log(new Person().sn); //after instantiation this points to The current object, so the current object has the sn property
console.log(new Person().gets());
console.log(new Person());

//After the instance method with the same name is defined on the instance, prototype reference and "this" at the same time, which one will the instance call first?

  1. var BaseClass = function() {    
  2. this.method1 = function(){    
  3.        alert(' Defined by the "this" in the instance method');    
  4.  }    
  5. };    
  6. var instance1 = new BaseClass();    
  7. instance1.method1 = function(){    
  8.     alert(' Defined directly in the instance method');    
  9. }    
  10. BaseClass.prototype.method1 = function(){    
  11.     alert(' Defined by the prototype instance method ');    
  12. }    
  13. instance1.method1();//Defined directly in the instance method    

    通过运行结果跟踪测试可以看出直接定义在实例上的变量的优先级要高于定义在“this”上的,而定义在“this”上的又高于 prototype定义的变量。即直接定义在实例上的变量会覆盖定义在“this”上和prototype定义的变量,定义在“this”上的会覆盖prototype定义的变量


总结:JS静态方法或静态属性只能由JS函数(理解为类)调用,实例方法或者实例属性只能由JS实例对象调用。

======================================================
函数实例化过程:
构造函数不需要显示的返回值。使用new来创建对象(调用构造函数)时,如果return的是非对象(数字、字符串、布尔类型等)会忽而略返回值;如果return的是对象,则返回该对象。
下面简单介绍下,javascript中new对象的过程:如var myObj = newPerson(“aty”,25);
1.创建一个空的Object对象.var obj = new Object();
2.将构造函数Person中this指向刚创建的obj对象
3.将创建的obj的__proto__指向构造函数Person的prototype。这一步是建立对象和原型直接的对应关系。firefox下通过
对象的__proto__属性能够访问到原型,IE下则没有暴露出相应的属性。
4.执行构造函数Person()中的代码


//JS 函数本身就是一个构造函数,和匿名构造函数Function 是一样的,但是Function需要new才产生一个匿名函数,
new操作符具体干了什么呢?其实很简单,就干了三件事情。
function Base(){}
var obj = new Base();
var obj  = {};
obj.__proto__ = Base.prototype;
Base.call(obj);   // 非常重要:将this对象的指向改变
/*第一行,我们创建了一个空对象obj
第二行,我们将这个空对象的__proto__成员指向了Base函数对象prototype成员对象
第三行,我们将Base函数对象的this指针替换成obj,然后再调用Base函数,于是我们就给obj对象赋值了一个id成员变量,这个成员变量的值是”base”,关于call函数的用法。*/


//Function构造函数,new Function 创建匿名函数
var BaseClass = new Function;
var Class2 = BaseClass;  
BaseClass.f1 = function(){  
console.log("BaseClass ' s static method");  
}  
Class2.f2 = function(){  
console.log("Class2 ' s static method");  
}  
BaseClass.f1();//BaseClass ' s static method  
BaseClass.f2();//Class2 ' s static method  
Class2.f1();//BaseClass ' s static method  
Class2.f2();//Class2 ' s static method  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325410179&siteId=291194637