call方法
使用 call() 方法,您可以编写能够在不同对象上使用的方法
通过 call(),您能够使用属于另一个对象的方法。
var person = {
fullName: function(city, country) {
return this.firstName + " " + this.lastName + "," + city + "," + country;
}
}
var person1 = {
firstName:"Bill",
lastName: "Gates"
}
// 输出 "Bill Gates,Seattle,USA"
person.fullName.call(person1, "Seattle", "USA");
ES6的import与export
1、在一个文件或模块中,export、import可以有多个,export default仅有一个
2、通过export方式导出,在导入时要加{},export default则不能加
export const str = 'hello world’;
export function f(a){ return a+1};
import { str, f } from 'demo1'; //也可以分开写两次,导入的时候带花括号
export default const str = 'hello world’;
import str from 'demo1' //导入的时候没有花括号
事件监听
Bus.js
class Bus {
constructor() {
// 收集订阅信息,调度中心
this.list = {};
}
// 订阅
$on(name, fn) {
this.list[name] = this.list[name] || [];
this.list[name].push(fn);
}
// 发布
$emit(name, data) {
if (this.list[name]) {
this.list[name].map((fn) => {
fn(data);
});
}
}
// 取消订阅
$off(name) {
if (this.list[name]) {
delete this.list[name];
}
}
}
export default Bus;
EventCenter.js
import Bus from "./Bus";
export default new Bus();
// import Vue from "vue";
// const bus = new Vue();
// export default bus;
网友评论