javaScript之面向对象编程

[size=medium][b]一、自定义构造函数[/b][/size]

即:通过使用 new 操作符调用函数来构造对象。


//定义一个函数
var Person = function(name){
this.name = name;
this.sayHello = function(){
return "Hi, my name is " + this.name + ".";
}
}

//使用 new 操作符调用函数
var tom = new Person("Tom");
tom.sayHello(); // "Hi, my name is Tom."

当使用 new 操作符调用函数时,将发生一下情况:

1. 创建一个空对象 {} ,并且 this 引用该对象。
2. 为该对象增加一个属性:__proto__ 。
该属性指向函数原型的引用。

函数原型也是一个空对象,在函数创建时被创建,
同时被增加一个 constructor 属性,指向函数自身。
var H = function(){};
console.log( H == H.prototype.constructor); // true

3. 如果函数没有写return语句的话,返回新创建的对象。
注意:如果函数中没有定义this,
该空对象(返回值)至少包含一个属性:__proto__



[size=medium][b]二、构造函数的返回值[/b][/size]

如果构造函数没有 return 语句,则隐式返回创建的那个新对象。
但是可以根据需要返回任意对象。(需要写return语句)


var H = function(){
this.name = "this is this.";

var that = {};
that.name = "that!";

return that;
}

//test
var h = new H();
h.name; // that!


这种写法的问题在于:新生成的对象丢掉了函数的原型链接。



[size=medium][b]三、技巧:自调用构造函数(强制使用 new 操作符)[/b][/size]

有时候忘记使用 new 操作符,会出现意想不到的结果。
    
function H(){
this.name = "Jake";
}

var h = H();
console.log(h); // undefined



可以使用 instanceof 关键字 + 递归调用 解决。
    
function H(){
if(!(this instanceof H)){
return new H();
}
this.name = "Jake";
}

//test
var h1 = new H(),
h2 = H();

console.log(h1.name); // Jake
console.log(h2.name); // Jake




—————————————

javascript 函数基础系列文章

[url=http://lixh1986.iteye.com/blog/1955314]1、JavaScript之变量的作用域[/url]
[url=http://lixh1986.iteye.com/blog/2028899]2、javascript之变量类型与变量声明及函数变量的运行机制[/url]
[url=http://lixh1986.iteye.com/blog/1947017]3、javaScript之function定义[/url]
[url=http://lixh1986.iteye.com/blog/1896682]4、javascript之function的prototype对象[/url]
[url=http://lixh1986.iteye.com/blog/1891833]5、javascript之function的(closure)闭包特性[/url]
[url=http://lixh1986.iteye.com/blog/1960343]6、javascript之function的this[/url]
[url=http://lixh1986.iteye.com/blog/1943409]7、javascript之function的apply(), call()[/url]


___________


javascript 面向对象编程系列文章:

[url=http://lixh1986.iteye.com/blog/1958956]1、javaScript之面向对象编程[/url]
[url=http://lixh1986.iteye.com/blog/2332467]2、javascript之面向对象编程之属性继承[/url]
[url=http://lixh1986.iteye.com/blog/2348442]3、javascript之面向对象编程之原型继承[/url]


-


-
引用请注明,
原文出处: http://lixh1986.iteye.com/blog/1958956


-


引用:

[url=http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html]Javascript 面向对象编程(一):封装[/url]
[url=http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html]Javascript面向对象编程(二):构造函数的继承[/url]
[url=http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.html]Javascript面向对象编程(三):非构造函数的继承[/url]


______________________________________________________________________________
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值