<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OOP</title>
</head>
<body>
<!-- 1.OOP的语法
2.OOP的思想 -->
<!-- OOP:提升效率 -->
</body>
<script>
// 语法
// 1.new Object()
// var obj = new Object();
// obj.name = '肉食动物';
// obj.ns = 2;
// obj.sum = function(a,b,d){
// // this指向函数的拥有者
// return a+b+c+this.ns;
// }
// 2.JSON
var shz={
description:"105个男人与3个女人的故事",
sale:function(price){
console.log(price);
},
author:"施耐庵"
}
// 3.工厂模式
// function createBook(author,price,type){
// return{
// author:author,
// price:price,
// type:type
// }
// }
// var b=createBook("我替你记得",25,"叙事");
// 4.构造函数
// 构造函数:开辟内存空间
// 析构函数:释放内存空间
// function Book(author,price,type){
// this.author=author;
// this.price=price;
// this.type=type;
// 不要把函数或公共资源写在构造函数里
// 容易造成内存的浪费
// this.sale=function(){
// console.log(this.price);
// }
// }
//公共资源写在原型中
// 原型需要使用构造函数的名字来调用
//原型是一块独立的内存空间,本质是个对象
// Book.prototype.sale=function(){
// console.log(this.price);
// }
// var hlm = new Book("曹雪芹",30,"小说");
// var sg=new Book("罗贯中",30,"小说");
// console.log(hlm.sale === sg.sale);
// this指向window
// var timer = setInterval(function(){
// console.log(this);
// },30)
// 三大特征:封装 将一堆属性方法结合在一起 继承 多态 多种形态 参数不能重复
function Animal(name,age){
this.name=name;
this.age=age;
}
Animal.prototype.run=function(){
}
function Dog(name,age,type){
// 构造函数主要通过 call()或者 apple()
//call(this,参数1,参数2,....)
//apply(this,[参数列表])
// Animal.call(this,name,age);
Animal.apply(this,[name,age]);
}
//原型的继承
//这个是浅拷贝,不要这样写
// Dog.prototype=Animal.prototype;
//需要使用深拷贝来完成继承
//1.for in
// for(x in Animal.prototype){
// Dog.prototype[x]=Animal.prototype[x];
// }
// 2.JSON
Dog.prototype=JSON.parse(JSON.stringify(Animal.prototype))
Dog.prototype.run=function(){
console.log("猎犬跑的就是快");
}
var d=new Dog("疙瘩",2,"金毛");
d.run();
//for in只能对第一层深拷贝,
//如果里面的数据是引用数据类型,
//就是浅拷贝了
//深拷贝使用JSON(或者es6时进行补充)
var obj2=JSON.parse(JSON.stringify(obj1))
//1.数组去重
// 第二种
Array.prototype.singleArr=function(){
var newArr=[];
this.forEach(function(item){
newArr.indexOf(item)===-1?newArr.push(item):null;
})
return newArr;
}
//2.反转字符串
function reverseString(str){
return str.split('').reverse().join('');
}
String.prototype.reverseStr=function(){
return this.split('').reverse().join('');
}
//3.一个数字的n次方
// function fang(n,m){
// return Math.pow(n,m)
// }
// var a = n ** m; n的m次方
</script>
</html>