new运算符
- 1.执行函数 2.自动创建一个空对象 3.把创建的对象指向另一个对象
- 4.把空对象和函数的this链接起来(this指向实例化对象) 5.隐式返还this
- 6.简化工厂模式;----->构造函数
function Tab(){
this.name = "张三";
}
Tab.prototype.hobby = function(){
console.log("hobby.....",this.name);
}
let tab1 = new Tab()
console.log(tab1.name);
tab1.hobby();
function mynew(constructor,...arg){
let obj={},
constructor.call(obj,...arg);
obj.__proto__ = constructor.prototype;
return obj;
}
拖拽
<html>
<head>
<style>
.mydiv1{
width:100px;
height:100px;
background:red;
position:absolute;
}
</style>
</head>
<body>
<div class="mydiv1"></div>
</body>
<script>
let mydiv1 = document.querySelector(".mydiv1");
mydiv1.onmousedown = e=>{
let ev = e||window.event;
let x =ev.clientX - mydiv1.offsetLeft;
let y =ev.clientY - mydiv1.offsetTop;
mydiv1.onmousemove = e=>{
let ev = e||window.event;
let xx = ev.clientX;
let yy = ev.clientY;
mydiv1.style.left = xx-x+"px";
mydiv1.style.top = yy-y+"px";
}
document.onmouseup = function(){
mydiv1.onmousemove = "";
}
}
</script>
</html>
拖拽面向对象封装
<html>
<head>
<style>
.mydiv1{
width:100px;
height:100px;
background:red;
position:absolute;
}
.mydiv2{
width:100px;
height:100px;
background:blue;
position:absolute;
left:300;
}
</style>
</head>
<body>
<div class="mydiv1"></div>
<div class="mydiv2"></div>
</body>
<script>
function Drag(ele){
this.ele = ele;
this.downFn();
}
Drag.prototype.downFn = function(){
this.ele.onmousedown = e=>{
let ev = e||window.event;
let x =ev.clientX - this.ele.offsetLeft;
let y =ev.clientY - this.ele.offsetTop;
this.moveFn(x,y);
this.upFn();
}
}
Drag.prototype.moveFn = function(x,y){
this.ele.onmousemove = e=>{
let ev = e||window.event;
let xx = ev.clientX;
let yy = ev.clientY;
this.ele.style.left = xx-x+"px";
this.ele.style.top = yy-y+"px";
}
}
Drag.prototype.upFn = function(){
this.ele.onmouseup = ()=>{
this.ele.onmousemove = "";
}
}
let mydiv1 = document.querySelector(".mydiv1");
let mydiv1 = document.querySelector(".mydiv2");
let drag1 = new Drag(mydiv1);
let drag1 = new Drag(mydiv2);
</script>
</html>
es5普通继承
function Dad(height){
this.name = "张三";
this.age = 20;
this.height = height;
this.money = "$11000000";
}
function Son(height){
Dad.call(this,height);
}
let newSon = new Son("178cm");
console.log(newSon);
es5原型组合继承
function Dad(height){
this.name = "张三";
this.age = 20;
this.height = height;
this.money = "$11000000";
}
Dad.prototype.hobby = function(){
console.log("喜欢高尔夫");
}
function Son(height){
Dad.call(this,height);
}
let Link = function(){};
Link.prototype = Dad.prototype;
Son.prototype = new Link();
Son.prototype.constructor = Son;
Son.prototype.hobby = function(){
console.log("篮球")
}
let newSon = new Son("178cm");
console.log(newSon);
深拷贝
function deepCopy(obj){
let newObj = Array.isArray(obj)?[]:{};
for(let i in obj){
if(obj.hasOwnProperty(i)){
if(typeof obj[i]==="object"){
if(obj[i] ===null){
newObj[i] = null;
}else{
newObj[i] = deepCopy(obj[i]);
}
}else{
newObj[i] = obj[i];
}
}
}
return newObj;
}
深拷贝继承
function LimitDrag(ele){
Drag.call(this.ele);
}
LimitDrag.prototype = deepCopy(Drag.prototype);
LimitDrag.prototype.constructor = LimitDrag;
LimitDrag.prototype.setStyle = function(leftNum,topNum){
leftNum = leftNum<0?0:leftNum;
topNum = topNum<0?0:topNum;
this.ele.style.left = leftNum + "px";
this.ele.style.top = topNum +"px";
}
let mydiv2 = document.querySelector(".mydiv2");
let drag2 = new LimitDrag(mydiv2);
function deepCopy(obj){
let newObj = Array.isArray(obj)?[]:{};
for(let i in obj){
if(obj.hasOwnProperty(i)){
if(typeof obj[i]==="object"){
if(obj[i] ===null){
newObj[i] = null;
}else{
newObj[i] = deepCopy(obj[i]);
}
}else{
newObj[i] = obj[i];
}
}
}
return newObj;
}
原型链
- 构造函数在查找时先在自身查找返回,再在原型中查找
- 原型本质也是一个对象,也由自身和原型构成,像链式一样;
es6类
function Tab(){
this.name = "张三";
}
Tab.prototype.hobby = function(){
console.log("篮球");
}
class Tab{
constructor(){
this.name = "张三";
}
hobby(){
console.log("篮球");
}
}
Tab.height = "178cm";
console.log(Tab.height);
let tab1 = new Tab();
es6继承类
class Tab{
static height = "178cm"
constructor(age){
this.name = "张三";
this.age = age;
}
hobby(){
console.log("篮球");
}
}
class LimitTab extends Tab{
constructor(age){
super(age);
}
hobby(){
super.hobby();会将父类的逻辑拿过来
console.log("足球")
}
}
console.log(LimitTab.height);
let tab2 = new LimitTab(20);
console.log(tab2.age);
tab2.hobby();
限定范围拖拽(通过继承实现)
<html>
<head>
<style>
.mydiv1{
width:100px;
height:100px;
background:red;
position:absolute;
}
.mydiv2{
width:100px;
height:100px;
background:blue;
position:absolute;
left:300;
}
</style>
</head>
<body>
<div class="mydiv1"></div>
<div class="mydiv2"></div>
</body>
<script>
function Drag(ele){
this.ele = ele;
this.downFn();
}
Drag.prototype.downFn = function(){
this.ele.onmousedown = e=>{
let ev = e||window.event;
let x =ev.clientX - this.ele.offsetLeft;
let y =ev.clientY - this.ele.offsetTop;
this.moveFn(x,y);
this.upFn();
}
}
Drag.prototype.moveFn = function(x,y){
this.ele.onmousemove = e=>{
let ev = e||window.event;
let xx = ev.clientX;
let yy = ev.clientY;
this.ele.style.left = xx-x+"px";
this.ele.style.top = yy-y+"px";
}
}
Drag.prototype.upFn = function(){
this.ele.onmouseup = ()=>{
this.ele.onmousemove = "";
}
}
let mydiv1 = document.querySelector(".mydiv1");
let drag1 = new Drag(mydiv1);
function LimitDrag(ele){
Drag.call(this.ele);
}
let Link = function(){};
Link.prototype = Drag.prototype;
LimitDrag.prototype = new Link();
LimitDrag.prototype.constructor = LimitDrag;
LimitDrag.prototype.setStyle = function(leftNum,topNum){
leftNum = leftNum<0?0:leftNum;
topNum = topNum<0?0:topNum;
this.ele.style.left = leftNum + "px";
this.ele.style.top = topNum +"px";
}
let mydiv2 = document.querySelector(".mydiv2");
let drag2 = new LimitDrag(mydiv2);
class Drag{
constructor(ele){
this.ele = ele;
this.downFn();
}
downFn(){
this.ele.onmousedown = e=>{
let ev = e||window.event;
let x =ev.clientX - this.ele.offsetLeft;
let y =ev.clientY - this.ele.offsetTop;
this.moveFn(x,y);
this.upFn();
}
}
moveFn(x,y){
this.ele.onmousemove = e=>{
let ev = e||window.event;
let xx = ev.clientX;
let yy = ev.clientY;
this.ele.style.left = xx-x+"px";
this.ele.style.top = yy-y+"px";
}
}
setStyle(leftNum,topNum){
this.ele.style.left = leftNum + "px";
this.ele.style.top = topNum +"px";
}
upFn(){
document.onmouseup = ()=>{
this.ele.onmousemove = "";
}
}
}
let mydiv1 = document.querySelector(".mydiv1");
let drag1 = new Drag(mydiv1);
class LimitDrag extends Drag{
constructor(ele){
super(ele);
}
setStyle(leftNum,topNum){
leftNum = leftNum<0?0:leftNum;
topNum = topNum<0?0:topNum;
super.setStyle(leftNum,topNum);
}
}
let mydiv2 = document.querySelector(".mydiv2");
let drag2 = new LimitDrag(mydiv2);
</script>
</html>
包装对象
几个方法
- hasOwnProperty() 判断是不是对象自身的属性,返回布尔值
- constructor 判断实例化对象的指向,返回布尔值
- 精确判断类型 Object.prototype.toString.call(arr);