Static and Instance Methods in JavaScript
class.method/instance method
https://abdulapopoola.com/2013/03/30/static-and-instance-methods-in-javascript/
在阅读vue示例代码时,经常看到Vue.xx函数或者$vm.yy函数,不太清楚这两者之间有什么区别。
google以后发现实际上还是有本质的区别的。
我们知道javascript的继承模型和java,php等面向对象的编程语言有非常大的差别
js是一个弱典型的类型语言,class类并不是真正的class,实际上class就是本身也是object的construct functions.我们通过使用new constructor调用来创建新的对象,这实际上某种意义上模拟了OOP.但是请记住:js的继承模型和传统的javas, php是不同的!
首先有static property/instance property这个概率。所有对象属性(this.xx)都为public的,可以通过thisobj.xx直接访问。当然我们可以通过在constructor function(也就是class类定义)中增加var关键字使得相应属性变为private的,对于这种private类型的属性,我们必须通过定义accessors和setters才能够访问。
//Constructor
var Person = function (name, age){
//private properties
var priv = {}; //Public properties
this.name = name;
this.age = age; //Public methods
this.sayHi = function(){
alert('hello');
}
} // A static method; this method only
// exists on the class and doesn't exist
// on child objects
Person.sayName = function() {
alert("I am a Person object ;)");
}; // An instance method;
// All Person objects will have this method
Person.prototype.setName = function(nameIn) {
this.name = nameIn;
} // Tests
var per = new Person('John Doe', 22); //Shows alert
Person.sayName(); //TypeError: Object [object Object] has no method 'sayName'
per.sayName() //Show alert
per.sayHi(); //John Doe
per.name; //
per.age; per.setName('Jane Doe'); //Jane Doe
per.name;
需要注意的是定义在类(构造函数)的属性上的static属性或者方法是不能在instance实例的上下文中访问的。但是java语言在这一点上就有所不同,在java中静态方法或者属性是可以在实例的上下文环境中访问的。这看起来非常奇怪,因为实例objects对象中并没有那个变量
这里我引用一下java的相关说明:
"
你也可以通过对象引用的模式来访问静态方法,比如:
myBike.numberOfBicycles
"
ES6中class method和instance method的对比
// es6 javascript code
class Foo {
bar() {...} static baz() {...}
}
const f = new Foo()
f.bar()
Foo.baz
foo = {bar: 'baz'};
console.log(foo.bar); // logs "baz" // none-es6 javascript code
foo = {};
console.log(foo.bar); // logs undefined function Foo(){}
Foo.prototype = {bar: 'baz'};
f = new Foo();
console.log(f.bar);
// logs "baz" because the object f doesn't have an attribute "bar"
// so it checks the prototype
f.bar = 'buzz';
console.log( f.bar ); // logs "buzz" because f has an attribute "bar" se
Foo.talk = function () {
alert('hello world!');
};
Foo.talk()
以Vue为例再看Vue.method/vueComponent.method
最后,我们来看Vuejs plugin中的几种用法,大家可以对照上面的所谓static和instance method来看:
MyPlugin.install = function (Vue, options) {
// 1. add global method or property
Vue.myGlobalMethod = function () {
// some logic ...
}
// 2. add a global asset
Vue.directive('my-directive', {
bind (el, binding, vnode, oldVnode) {
// some logic ...
}
...
})
// 3. inject some component options
Vue.mixin({
created: function () {
// some logic ...
}
...
})
// 4. add an instance method
Vue.prototype.$myMethod = function (methodOptions) {
// some logic ...
}
}
Static and Instance Methods in JavaScript的更多相关文章
- Mongoose 'static' methods vs. 'instance' methods
statics are the methods defined on the Model. methods are defined on the document (instance). We may ...
- Java SE 8 docs——Static Methods、Instance Methods、Abstract Methods、Concrete Methods和field
一.Static Methods.Instance Methods.Abstract Methods.Concrete Methods ——Static Methods:静态方法 ——Instance ...
- Core Java Volume I — 4.4. Static Fields and Methods
4.4. Static Fields and MethodsIn all sample programs that you have seen, the main method is tagged w ...
- Swift编程语言学习12 ——实例方法(Instance Methods)和类型方法(Type Methods)
方法是与某些特定类型相关联的函数.类.结构体.枚举都能够定义实例方法:实例方法为给定类型的实例封装了详细的任务与功能.类.结构体.枚举也能够定义类型方法:类型方法与类型本身相关联.类型方法与 Obje ...
- UIView 实例方法 Instance Methods(转)
好了,我接着上篇,开始我们的对UIView 实例方法的探索 UIView 实例方法 Instance Methods 初始化一个视图 - (id)initWithFrame:(CGRect)aRect ...
- Instance Methods are Curried Functions in Swift
An instance method in Swift is just a type method that takes the instance as an argument and returns ...
- A Simple Example About Privileged Methods in JavaScript
Douglas Crockford classified the "class methods" in JavaScript into three types: private, ...
- Difference Between static and default methods in interface
I was learning through interfaces when I noticed that you can now define static and default methods ...
- Static Fields and Methods
If you define a field as static, then there is only one such field per class. In contrast, each obje ...
随机推荐
- 【会话技术】Cookie技术 案例:访问时间
创建时间:6.30 代码: package cookie; import java.io.IOException; import java.text.SimpleDateFormat; import ...
- Android端项目测试
目录 一.概述 二.使用工具 三.测试 1.测试主要的两大功能 进入主界面,测试排行榜查看功能是否能运行 测试83端口打卡能否运行 修改个人信息已经注册功能 2.测试参数是否正确 3.测试刷新能否使用 ...
- debug模式不报错,release模式报错
经常会 char * pMem = new char[icount]; 其中icount为变量,然后对该内存段猛的操作.release编译出来,出现莫名奇妙的错误.但是debug没问题. 后面查了别人 ...
- celery异步任务、定时任务
阅读目录 一 什么是Celery? 二 Celery的使用场景 三 Celery的安装配置 四 Celery异步任务 五Celery定时任务 六在Django中使用Celery 一 什么是Cele ...
- zabbix-钉钉报警媒介
(1)第三方报警平台(钉钉) 先指定要发送的群,在群里创建机器人 添加机器人 可以参考 “说明文档” 创建测试文档 vim ceshi.sh curl 'https://oapi.dingta ...
- GLIBC中的库函数fflush究竟做了什么?
目录 目录 1 1. 库函数fflush原型 1 2. FILE结构体 1 3. fflush函数实现 2 4. fclose函数实现 4 附1:强弱函数名 5 附2:属性__visibility__ ...
- memcpy和strcpy的区别
strcpy和memcpy主要有以下3方面的区别. 复制的内容不同.strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组.整型.结构体.类等. 复制的方法不同.strcpy不需要指 ...
- ZROI 暑期高端峰会 A班 Day6 离线问题
FBI Warning:本文含有大量人类本质之一. 动态联通问题 允许离线. 模板,不讲了. 归并排序 %@)(#&%)++%($@)%!#(&%)(&@))) 主定理 U^( ...
- 接口自动化框架2-升级版(Pytest+request+Allure)
前言: 接口自动化是指模拟程序接口层面的自动化,由于接口不易变更,维护成本更小,所以深受各大公司的喜爱. 第一版入口:接口自动化框架(Pytest+request+Allure) 本次版本做了一些升级 ...
- Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins:mav问题
1.导致问题原因:从装系统,从win7改到win10 由于重装了系统,打开eclipse时,maven验证会出错,点击pom文件,会发现有红色的Cannot read lifecycle mappin ...