Javascript进阶(4)---几种特殊情况分析
情况一
function AAA(){ this.say = "haha"; } AAA.ownPro = "own"; var obj = new AAA(); console.log(obj.say); //"haha" console.log(obj.own); //undefined console.log(AAA.ownPro); //"own"
情况二
var Person = function (){ this.pro= "qqq"; this.sayword = function (){ return 789; }; } var Carl = new Person (); console.log (Carl.pro); //"qqq" Person.prototype.newPro = 123; console.log(Person.sayword); //undefined 这里表示访问的是私有属性,并不是function里面this声明的公有属性 console.log(Carl.newPro); //123 console.log(Carl.sayword); // function(){return 789;}
情况三
function obj(){ var a = 444; this.b = 456; c = function (){ var q = 123; } d=456; obj.e=741; } var new_obj = new obj(); console.log(new_obj.a); //undefined console.log(new_obj.b); //456 访问使用this定义的公共属性 console.log(new_obj.c); //undefined console.log(new_obj.d); //undefined console.log(new_obj.e); //undefined console.log(obj.a); //undefined console.log(obj.b); //undefined console.log(obj.c); //undefined console.log(obj.d); //undefined console.log(obj.e); //741
- 定义一个函数之后,通过“ . ”号为其添加的属性和函数,通过自身可以访问到 obj.e, 但是这个函数创建出来的实例却访问补刀,我们将这种属性称之为静态变量、活着是静态属性
情况四
function obj() { this.a = 123; this.b = 456; this.c = function(){ return 1234; } } var obj1 = new obj(); obj1.a = 456; obj1.c = {}; //检查是否修改成功 console.log(obj1.a); //456 console.log(obj1.c); //Object var obj2 = new obj(); //检查是否修改成功 console.log(obj2.a); //123 console.log(obj2.c); //function(){ return 1234;}
- 结果可见obj1的属性修改对obj2中的属性完全无影响,也就是说obj1与obj2中的属性虽同名但不是一个引用