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对象的值,以扩充函数赖 ...
随机推荐
- js中(function(){})()的写法用处
直到今天我才明白的一个玩意!!! 来来来,首先嘛,JS中函数有两种命名方式 1.一种是声明式. 而声明式会导致函数提升,function会被解释器优先编译.即我们用声明式写函数,可以在任何区域声明,不 ...
- ihandy2019笔记编程真题
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Java与Go语言差异1 传值还是传引用
在Java中,复杂类型(除原始类型外的其它类)作为入参,在方法中被修改后,跳出方法对象内的值仍会保持,也就是传的是引用.原始类型传的是值,如int, double等原始类型. Java代码: publ ...
- Win10查看屏保的所在位置
路径 C:\Users\Hlzy\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\Local ...
- npm run build 打包后空白页解决问题两种情况
问题一:assetsPublicPath配置错误 解决办法:打开config/index.js文件 build:{ // assetsPublicPath: '/' ass ...
- 虚拟机(linux)怎么上网
问题描述:本机并没有显示虚拟机(linux)的虚拟网卡,那能不能用虚拟机上网呢,如果要让本机显示出虚拟机的虚拟网卡会有一万步各种安装卸载,那么,在现有条件下可不可以上网呢,答案是可以的. 解决方案: ...
- 【sklearn文本特征提取】词袋模型/稀疏表示/停用词/TF-IDF模型
1. 词袋模型 (Bag of Words, BOW) 文本分析是机器学习算法的一个主要应用领域.然而,原始数据的这些符号序列不能直接提供给算法进行训练,因为大多数算法期望的是固定大小的数字特征向量, ...
- 论文阅读笔记(二十二)【CVPR2017】:See the Forest for the Trees: Joint Spatial and Temporal Recurrent Neural Networks for Video-based Person Re-identification
Introduction 在视频序列中,有些帧由于被严重遮挡,需要被尽可能的“忽略”掉,因此本文提出了时间注意力模型(temporal attention model,TAM),注重于更有相关性的帧. ...
- SpringBoot原理—分析SpringBoot启动机制(starter机制)
一:前言使用过springboot的同学应该已经知道,springboot通过默认配置了很多框架的使用方式帮我们大大简化了项目初始搭建以及开发过程.本文的目的就是一步步分析springboot的启动过 ...
- kvm虚拟化安装及虚拟机创建
1.开启虚拟化(VMware虚拟机为例),安装centos7操作系统. 2.查看CPU是否开启虚拟化 #zgrep "vmx" /proc/cpuinfo 3.配置yum源,通过 ...