码迷,mamicode.com
首页 > 编程语言 > 详细

javascript的面向对象用法

时间:2015-06-09 14:13:38      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:javascript面向对象使用方法

Javascript的面向对象使用方法简单介绍如下:
            
            
           //声明构造Person类。使用function作为构造方法。

      function Person(){

  }

      //Person类的公共属性。使用.prototype声明。

  Person.prototype = {

name:"张三",

age:22,

gender:"男",

eat:function(s){

alert("我吃:" + s);

}

  };

  //构造对象。

  var p = new Person();

       alert(p.eat("apple"))//输出:"我吃:apple"

 

       

  function User(pwd){

  var passwd = pwd ;//私有的属性

  function getPwd(){//私有的调用方法

return passwd ;

  }

  

  this.pwdService = function(){//特权函数(共有方法通过特权方法访问私有属性)

return getPwd();

  }

  }


  User.prototype.check = function(p){//共有方法

return this.pwdService() == p;

  }


  var u = new User("123");

  alert(u.passwd)//输出:"undifined"

  alert(u.pwdService())//输出:"123"

  alert(u.check("123"))//输出:"true"


javascript的面向对象用法

标签:javascript面向对象使用方法

原文地址:http://iamevergreen.blog.51cto.com/6848524/1659883

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!