Summary
- private variables are declared with the 'var' keyword inside the object, and can only be accessed by private functions and privileged methods.
- private functions are declared inline inside the object's constructor (or alternatively may be defined via
var functionName=function(){...}
) and may only be called by privileged methods (including the object's constructor).
- privileged methods are declared with
this.methodName=function(){...}
and may invoked by code external to the object.
- public properties are declared with
this.variableName
and may be read/written from outside the object.
- public methods are defined by
Classname.prototype.methodName = function(){...}
and may be called from outside the object.
- prototype properties are defined by
Classname.prototype.propertyName = someValue
- static properties are defined by
Classname.propertyName = someValue
Example
In this example, a person's name and race are set at birth and may never be changed. When created, a person starts out at year 1 and a hidden maximum age is determined for that person. The person has a weight which is modified by eating (tripling their weight) or exercising (halfing it). Every time the person eats or exercises, they grow a year older. The person object has a publicly accessible 'clothing' property which anyone can modify, as well as a dirtFactor which can be modified manually (throwing dirt on or scrubbing it off), but which increases every time the person eats or exercises, and is reduced by the use of the shower() method.
js 代码
- function Person(n,race){
- this.constructor.population++;
-
-
-
-
- var alive=true, age=1;
- var maxAge=70+Math.round(Math.random()*15)+Math.round(Math.random()*15);
- function makeOlder(){
- return alive = (++age <= maxAge)
- }
- var myName=n?n:"John Doe";
- var weight=1;
-
-
-
-
-
- this.toString=this.getName=function(){ return myName }
- this.eat=function(){
- if (makeOlder()){
- this.dirtFactor++;
- return weight*=3;
- } else
- alert(myName+" can't eat, he's dead!");
- }
- this.exercise=function(){
- if (makeOlder()){
- this.dirtFactor++;
- return weight/=2;
- } else
- alert(myName+" can't exercise, he's dead!");
- }
- this.weigh=function(){ return weight }
- this.getRace=function(){ return race }
- this.getAge=function(){ return age }
- this.muchTimePasses=function(){ age+=50; this.dirtFactor=10; }
-
-
-
- this.clothing="nothing/naked";
- this.dirtFactor=0;}
-
-
-
- Person.prototype.beCool = function(){ this.clothing="khakis and black shirt" }
- Person.prototype.shower = function(){ this.dirtFactor=2 }
- Person.prototype.showLegs = function(){ alert(this+" has "+this.legs+" legs") }
- Person.prototype.amputate = function(){ this.legs-- }
-
-
-
- Person.prototype.legs=2;
-
-
-
- Person.population = 0;
-
- RunGavinsLife(){
- var gk=new Person("Gavin","caucasian");
- var lk=new Person("Lisa","caucasian");
- alert("There are now "+Person.population+" people");
- gk.showLegs(); lk.showLegs();
- gk.race = "hispanic";
- alert(gk+"'s real race is "+gk.getRace());
- gk.eat(); gk.eat(); gk.eat();
- alert(gk+" weighs "+gk.weigh()+" pounds and has a dirt factor of "+gk.dirtFactor);
- gk.exercise();
- gk.beCool();
- gk.clothing="Pimp Outfit";
- gk.shower();
- alert("Existing shower technology has gotten "+gk+" to a dirt factor of "+gk.dirtFactor);
- gk.muchTimePasses();
- Person.prototype.shower=function(){
- this.dirtFactor=0;
- }
- gk.beCool=function(){
- this.clothing="tinfoil";
- };
- gk.beCool();
- gk.shower();
- alert("Fashionable "+gk+" at " +gk.getAge()+" years old is now wearing " +gk.clothing+" with dirt factor " +gk.dirtFactor);
- gk.amputate();
- gk.showLegs(); lk.showLegs();
- gk.muchTimePasses();
- gk.eat();
- }
分享到:
相关推荐
ursa, node.js public/private 键的绑定 URSA/public/private 键 注意:这个软件包是从 medium 和 NodePrime转移到 quartzjer到 JoshKaufman on 1 -2017. 欢迎请求请求来帮助维护它。- -This MOD
SwiftyRSA, 在Swift中,RSA public/private 密钥加密 SwiftyRSA Swift中的 public 密钥RSA加密。 在将驱动程序编号提交到之前,在 Scoop iOS应用程序中使用SwiftyRSA来加密驱动程序许可证编号。安装 Swift 3.2/4. 0
在JavaScript中,public、private和static这些概念并非原生关键字,但可以通过特定的编码模式来实现类似的功能。在C#等静态类型语言中,public、private和static是访问修饰符,用于控制类成员的可见性和作用域。而在...
该文档记录了oracle11g rac修改public ip/private ip/vip的实施步骤
public class PullBlurScrollView extends ScrollView { /**头部**/ private View mHeader; /**主体**/ private View mContentView; /** 模糊图片 **/ private BlurDrawable mBlurDrawable; /** 阻尼系数,越...
以上就是关于面向对象编程中`public`、`private`、`protected`、`final`、`abstract`、`super`和`this`等关键字的详细介绍。这些概念是面向对象编程的基础,掌握它们有助于更好地理解和应用面向对象的设计原则和技术...
<script src="./public/js/jquery-ui-1.10.3.min.js"></script> <script src="./public/js/jquery.datepicker-zh-CN.js"></script> <link href="./public/css/jqueryui/jquery-ui-1.10.3.min.css" rel="stylesheet">...
### Delphi中的访问控制修饰符:private,public,protected 在面向对象编程中,封装是保护数据完整性和安全性的核心原则之一。通过控制类成员(属性和方法)的可见性,我们可以有效地管理类的内部状态,并确保外部...
### Delphi中的Public,Private,Protected,Published作用域详解 #### 一、引言 在面向对象编程中,封装是核心概念之一,它通过限制对类内部数据的直接访问来提高代码的安全性和可维护性。Delphi作为一种强大的...
public class CircleIndicator extends LinearLayout implements ViewPager.OnPageChangeListener{ private ViewPager mViewPager; ViewPager.OnPageChangeListener mListener; private final static int SCROLL_...
Java 中有四种访问修饰符:public、protected、默认(default)和 private。这四种修饰符的作用域从大到小依次降低。 1. public 修饰符 public 修饰符是最宽松的访问修饰符,它允许从任何地方访问修饰的成员,...
需要的朋友可以过来参考下class ValidateCode { ... public function __construct() { $this->font = dirname(__FILE__).'/font/elephant.ttf';//注意字体路径要写对,否则显示不了图片 }
例如,在`learn_public_private.cpp`文件中,我们可能会看到这样的代码: ```cpp class MyClass { public: int publicVar; // 公有变量 void publicFunc() { // 公有函数 // 函数体 } }; int main() { ...
Elixir由T Taylor Otwell开发,目的是简化常见的前端任务,如编译Sass、Less,或者处理JavaScript文件,使其在大型项目中更加便捷。在本教程中,我们将深入探讨如何使用Laravel Elixir来实现文件合并的功能。 标题...
public class Department { private int id; private String name; // private Map,Employee> emps; // private List<Employee> emps; private Set<Employee> emps; // private Employee[] emps; // //public Map...
<script src="./public/js/jquery-ui-1.10.3.min.js"></script> <script src="./public/js/jquery.datepicker-zh-CN.js"></script> <script src="./public/js/jquery-ui-timepicker-addon.js"></script> $( "#...
public View decorview; /** 接收定位界面城市数据 */ private static String citylocation; private Scoll scoll; /** 进度框 */ ProgressDialog pd; private static final String TAG = "demoActivity"; /...
这篇文章主要目的帮助理解JavaScript中的一些概念如:scope,closure, this, namespace, function scope, global scope, lexical scope and public/private scope. 希望从这篇文章中能回答如下的问题: 什么是作用域...
/// 在控件Init的时候将JS路径添加到HttpContext.Current.Items["IncludedJavaScript"]中。 /// /// protected override void OnInit(EventArgs e) { base.OnInit(e); if (!string....
对评论进行投票索菲亚工作文件: /routes/users.js /routes/vote/index.js /public/css/login.css /public/js/login.js /视图/登录.ejs查看评论,有或没有搜索过滤阿卜迪工作文件: /routes/reviews.js /public/css...