javascript当中静态方法和prototype用法
6)静态方法和prototype(难)
例 3.6.1
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<script>
/*note that 马克-to-win: static variable's value has nothing to do with instance's variable's value.instance 名称 can not 直接access static member like in java.
This is different from Java,比如下面例子中,Student.number=2,但是d1.number就为undefined.This is different from Java,但在实例方法中(比如d1.info)可以访问Student.number。这是和java中一样的。或者说function外或任何地方都可以访问Student.number。反过来,d1.age也可以在静态方法中访问,就像在function外一样,任何地方都能访问d1.age。String.prototype.abcd,这是给所有的实例加属性而不是静态属性。*/
function Student(number, agev)
{
this.age = agev;
/*static variable's value can not be accessed by instance */
Student.number = number;
/*lb is local variable, but not a member variable because it is not modified by this. from outside it can not be accessed. refer to noblockScope.html */
var lb = 0;
}
var d1 = new Student(1, 3);
document.writeln("this的age属性为means window.age" + this.age + "<br>");
document.writeln("d1的age属性为" + d1.age + "<br>");
document.writeln("d1的number属性为" + d1.number + "<br>");
document.writeln("通过Student访问静态number属性为" + Student.number + "<br>");
document.writeln("d1的lb属性为" + d1.lb + "<br><hr>");
d1.qixy = "abc";/*以随意为实例加属性或方法*/
document.writeln("可以随意为实例加属性或方法see following,d1的qixy属性为" + d1.qixy + "<br><hr>");
document.writeln("是否有静态变量qixy" + Student.qixy + "<br><hr>");
d1.info = function()/*此方法仅为d1对象所用*/
{
document.writeln("对象的qixy属性:" + this.qixy);
document.writeln("对象的age属性:" + this.age);
/*下列话是合法的, 因为不是this.number, 而是Student.number*/
document.writeln("static method is " + Student.number);
};
Student.prototype.infop = function()/*此方法可以为所有Student对象所用*/
{
document.writeln("对象的qixy属性p:" + this.qixy);
document.writeln("对象的age属性p:" + this.age);
文章转载自:https://blog.csdn.net/qq_44594249/article/details/100053745
javascript当中静态方法和prototype用法的更多相关文章
- JavaScript的apply()方法和call()方法
1 <script type="text/javascript"> 2 /*定义一个人类*/ 3 function Person(name,age) 4 { 5 thi ...
- js方法和prototype
JS中的方法可以分为三类 1.对象方法 2.类方法 3.原型方法 例: function People(name) { this.name=name; //对象方法 this.Introduce=fu ...
- Javascript中call方法和apply方法用法和区别
第一次在博客园上面写博客,知识因为看书的时候发现了一些有意思的知识,顺便查了一下资料,就发到博客上来了,希望对大家有点帮助. 连续几天阅读<javascript高级程序设计>这本书了,逐渐 ...
- javascript当中类型转换,typeof的用法
1)类型转换,typeof的用法 例 3.1.1 <HTML><head> <meta http-equiv="content-type" co ...
- 深入理解Java中的同步静态方法和synchronized(class)代码块的类锁
一.回顾学习内容 在前面几篇博客中我我们已经理解了synchronized对象锁.对象锁的重入.synchronized方法块.synchronized非本对象的代码块, 链接:https://www ...
- staticmethod()静态方法和classmethod类方法都是装饰器
1.staticmethod()静态方法 使用@staticmethod目的是为了增加可读性,不需要参数self(不强制要求传递参数) 的方法都可以加上@staticmethod增加可读性 静态方法无 ...
- Oracle 分页 ROWNUM 两种分页方法和ROWID用法
一 原因一 oracle默认为每个表生成rowmun,rowid字段,这些字段我们称之为伪列 测试表 CREATE TABLE A ( AID NUMBER() primary key, ANAME ...
- 关于JAVASCRIPT call 方法和 apply 方法性能对比
JavaScript 关于call 方法和 apply 方法常用形式 call obj.call(object, args , ....); apply obj.apply(object, [args ...
- call()方法和apply()方法用法总结
1. 每个函数都包含两个非继承而来的方法:call()方法和apply()方法. 2. 相同点:这两个方法的作用是一样的. 都是在特定的作用域中调用函数,等于设置函数体内this对象的值,以扩充函数赖 ...
随机推荐
- 后端跨域的N种方法
简单来说,CORS是一种访问机制,英文全称是Cross-Origin Resource Sharing,即我们常说的跨域资源共享,通过在服务器端设置响应头,把发起跨域的原始域名添加到Access-Co ...
- Java中Thread方法启动线程
public class ThreadTest extends Thread { private int count = 10; @Override public void run() { //重写 ...
- php ftp 使用 以及 php_connect_nonb() failed: Operation now in progress (115)
设置 ftp_pasv($conn,true); 会出现下面错误 不设置 调用ftp 连接没问题 ftp_nlist ftp_put ftp_get 等函数都不成功 ftp_nb_fput( ...
- win10 mysql数据库中文乱码
https://blog.csdn.net/weixin_41855029/article/details/80462234
- memcached的安装、常用命令以及在实际开发中的案例
Memcached注意缺乏安全认证以及安全管制需要将Memcached服务器放置在防火墙(iptables)之后 Linux平台 (CentOS)安装Memcached 安装依赖yum -y inst ...
- P5163 WD与地图 [整体二分,强连通分量,线段树合并]
首先不用说,倒着操作.整体二分来做强连通分量,然后线段树合并,这题就做完了. // powered by c++11 // by Isaunoya #include <bits/stdc++.h ...
- Java第五节课总结
继承是对现实生活中的“分类”概念的一种模拟. 通过surper调用的基类构造方法,必须是子类构造方法中的第一个语句. 构造函数(constructor)是一种特殊的方法 .主要用来在创建对象时初始化对 ...
- Codeforces Round #592 (Div. 2) D - Paint the Tree
题目链接:https://codeforces.com/contest/1244/problem/D 题意:给你一个树,让你把树上的每个节点染成三种颜色,使得任意三个互相相邻的节点颜色都不一样(意思是 ...
- 正则表达式[\w]+,\w+,[\w+]
正则表达式[\w]+,\w+,[\w+] 三者区别? [],[ABC]+,[\w./-]+ 表达什么? 正则表达式[\w]+,\w+,[\w+] 三者有何区别:[\w]+和\w+没有区别,都是匹配数字 ...
- pip淘宝镜像安装
pip install virtualenvwrapper-win pip install -i https://pypi.tuna.tsinghua.edu.cn/simple virtualenv ...