# 基础知识

# 封装Cookies

import Cookies from 'js-cookie'

const TokenKey = 'loginToken'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

export function setSJID(id) {
  return Cookies.set('SJID', id)
}

export function getSJID() {
  return Cookies.get('SJID')
}

# git

# git的使用方法

git status 查看git是否有修改内容需要提交 git add 指向需要提交的内容文件 git commit 提交到本地库 git push origin master 提交到远程仓库

# git的配置

1.设置用户名和邮箱(--global 为全局参数,表明本地所有Git仓库都会使用这个配置)

git config --global user.name "yourname"

git config --global user.email "your_email@youremail.com"

ssh-keygen -t rsa -C "your_email@youremail.com"

# 创建版本库

git init

# 连接远程仓库

git remote add origin git@github.com:yourName/repositoryname.git

如果报错执行

git remote rm origin

# 从远程仓库pull文件( 若远程仓库没有文件,直接执行步奏六)

git pull origin master

# 将本地文件push到远程仓库(若没有文件则手动创建)

git status          查看工作目录的状态

git add <file>        将文件添加到暂存区

git commit -m "commnet"   提交更改,添加备注信息(此时将暂存区的信息提交到本地仓库)

git push origin master    将本地仓库的文件push到远程仓库(若 push 不成功,可加 -f 进行强推操作)

# 相关问题

Q1. git pull origin master 无法进行pull,出现如下提示:

git pull origin master fatal: unable to access 'https://github.com/yourName/Demo.git': error setting certificate verify locations: CAfile: G:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt CApath: none 分析:ca-bundle.crt文件是证书文件。根据提示CApath:none 没有该文件,所以无法访问远程仓库

解决:修改为正确路径 或者 将证书验证设置false

git config --system http.sslcainfo E:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt

git config --system http.sslverify false Q2.git pull origin master 出现如下提示:

fatal: refusing to merge unrelated histories 解决:如下操作即可解决

git pull origin master --allow-unrelated-histories Q3.每次git push origin master 时都需要输入用户名和密码:

因为配置的时候使用的是https协议,所以每次都需要输入

git remote -v 查看远程连接

git remote rm origin 删除远程连接

git remote add origin git@github.com:yourName/repositoryname.git

222 error: src refspec master does not match any

执行git commit -m 'fist commit'

# vue 双向绑定


// 数组方法劫持
let oldProtoMehtods = Array.prototype;
let proto = Object.create(oldProtoMehtods);
['push', 'pop', 'shift', 'splice', 'unshift', 'reverse'].forEach(method => {
	var original = proto[method];

	Object.defineProperty(proto, method, {
		value: function() {

			var args = [...arguments];

			let result = original.apply(this, args);

			observe(args);

			return result;

		}
	})
})

/**
 * 数据驱动
 * @param {object} obj 数据劫持对象
 */
function observe(obj) {
	if (!isObject(obj)) return obj;

	if (Array.isArray(obj)) {
		Object.setPrototypeOf(obj, proto)
		for (let i = 0; i < obj.length; i++) {
			observe(obj[i])
		}
		return
	}

	for (const key in obj) {
		defineReactive(obj, key, obj[key])
	}
}

function isObject(obj) {
	return obj && typeof obj === 'object';
}


/**
 * 数据劫持
 * @param {object}} obj 劫持对象
 * @param {string} key 属性名
 * @param {object} value 属性值
 */
function defineReactive(obj, key, value) {
	observe(value);

	let dep = new Dep();

	Object.defineProperty(obj, key, {
		get() {
			if (Dep.target) {
				// 依赖收集
				dep.add()
			}
			return value;
		},
		set(nVal) {
			if (nVal !== value) {
				observe(value);
				value = nVal;

				// 通知更新,对应的更新视图
				dep.notify()
			}
		}
	})
}

/**
 * 依赖收集
 */
function Dep() {
	this.deppend = []
}

Dep.target = null;

Dep.prototype.add = function() {
	// 收集watcher
	this.deppend.push(Dep.target)
}
Dep.prototype.notify = function() {
	this.deppend.forEach((target) => {
		// 调用watcher的更新函数
		target.update()
	})
}

Dep.target = null;
// 依赖堆栈
var targetStack = [];

/**
 * 监视者入栈
 * @param {object} target watcher
 */
function pushTarget (target) {
  targetStack.push(target);
  Dep.target = target;
}

/**
 * 监视者出栈
 */
function popTarget () {
  targetStack.pop();
  Dep.target = targetStack[targetStack.length - 1];
}

/**
 * Watcher 监视者
 * @param {object} vm 监视对象
 * @param {string} expression 目标属性
 * @param {function} callback 回调函数
 */
function Watcher(vm, expression, callback) {
	this.vm = vm;
	this.expression = expression;
	this.callback = callback;
	this.value = this.getVal();
}

Watcher.prototype.getVal = function() {
	pushTarget(this);
	// 触发 get 方法,完成对 watcher 的收集
	let val = this.vm
	// 查询目标对象值
	this.expression.split('.').forEach((key) => {
		val = val[key]
	})
	popTarget()
	return val
}

Watcher.prototype.update = function() {
	let value = this.getVal();
	this.callback.call(this.vm, value, this.value);
}

let obj = [{
	id: 1,
	name: '张三',
	children: [{
		id: 2,
		name: '李四',
		children: [{
			id: 4,
			name: '麻子'
		}]
	}, {
		id: 3,
		name: '王二'
	}]
}];

observe(obj);
new Watcher(obj, '0.children.0.children.0.name', handler)

function handler(nval, oval) {
	console.log(nval, oval);
}
obj[0].children[0].children[0].name = '老王的儿子';

obj.push({
	id: 7,
	name: '小明'
})