JavaScript 高级之面向对象
1. 对象属性及方法
- 创建对象的方式
<script>
//创建对象的方式一
var obj = {};
//创建对象的方式一
var obj = new Object();
</script>
- 挂载在对象上的变量叫对象的属性;挂载在对象上的函数叫对象的方法
obj.name = "David";
obj.say = function () {
alert(this.name)
}
obj.say();
2. 创建对象的发展
- 普通创建模式
var obj1 = new Object();
obj1.name = "David";
obj1.say = function () {
alert(this.name)
}
obj1.say();
var obj2 = new Object();
obj2.name = "Mike";
obj2.say = function () {
alert(this.name)
}
obj2.say();
- 工厂模式
function create(name,age){
var obj = new Object();
obj.name = name;
obj.age = age;
obj.say = function () {
alert("姓名:"+ this.name + "年龄:" + this.age);
}
return obj;
}
var obj1 = create("David",22);
obj1.say();
var obj2 = create("Mike",18);
obj2.say();
- 构造函数模式
- obj1.say != obj2.say,同样的方法占用了两块内存
function create(name,age){
obj.name = name;
obj.age = age;
obj.say = function () {
alert("姓名:"+ this.name + "年龄:" + this.age);
}
}
var obj1 = new create("David",22);
obj1.say();
var obj2 = new create("Mike",18);
obj2.say();
- 原型模式
- obj1.say = obj2.say,同样的方法占用了一块内存,即两个对象共用了一个方法
- 不同的属性放在构造函数里,相同的方法放在原型对象上
- 原型对象也是对象
function create(name,age) {
obj.name = name;
obj.age = age;
}
create.prototype.say = function () {
alert("姓名:"+ this.name + "年龄:" + this.age);
}
var obj1 = new create("David",22);
obj1.say();
var obj2 = new create("Mike",18);
obj2.say();
3. 实现tab选项卡
- 面向过程实现tab选项卡
css代码
#big div{
border: 1px solid #000;
width: 200px;
height: 200px;
display: none;
}
#big .block{
display: block;
}
input.active{
background-color: red;
}
html代码
<div id = "big">
<input type = "button" value ="选项一" class = "active"/>
<input type = "button" value = "选项二"/>
<input type = "button" value = "选项三"/>
<div class = "block">我是内容一</div>
<div>我是内容二</div>
<div>我是内容三</div>
</div>
js代码
window.onload = function(){
var big = document.getElementById("big");
var inputs = big.getElementsByTagName("input");
var divs = big.getElementsByTagName("div");
for(var i = 0;i < inputs.length;i++){
inputs[i].index = i;
inputs[i].onclick = function(){
for(var j = 0;j < inputs.length;j++){
inputs[j].className = "";
divs[j].className = "";
}
this.className = "active";
divs[this.index].className = "block";
}
}
}
- 面向对象实现tab选项卡
css代码
#big1 div,#big2 div{
width: 200px;
height: 200px;
display: none;
border: 1px solid #000;
}
#big1 .block,#big2 .block{
display: block;
}
input.active{
background-color: red;
}
html代码
<div id = "big1">
<input type = "button" value = "选项一" class = "active"/>
<input type = "button" value = "选项二"/>
<input type = "button" value = "选项三"/>
<div class = "block">我是内容一</div>
<div>我是内容二</div>
<div>我是内容三</div>
</div>
<div id="big2">
<input type = "button" value = "选项一" class = "active"/>
<input type = "button" value = "选项二"/>
<input type = "button" value = "选项三"/>
<div class = "block">我是内容一</div>
<div>我是内容二</div>
<div>我是内容三</div>
</div>
js代码
window.onload = function(){
var tab1 = new Tab("big1");
var tab2 = new Tab("big2");
}
function Tab(id){
var big = document.getElementById(id);
this.inputs = big.getElementsByTagName("input");
this.divs = big.getElementsByTagName("div");
var that = this;
for(var i = 0;i < this.inputs.length;i++){
this.inputs[i].index = i;
this.inputs[i].onclick = function(){
that.show(this);
}
}
}
Tab.prototype.show = function(obtn){
for(var j = 0;j < this.inputs.length;j++){
this.inputs[j].className = "";
this.divs[j].className = "";
}
obtn.className = "active";
this.divs[obtn.index].className = "block";
}
4. 引用
- 引用赋值
// 引用赋值,共用一块内存空间
var arr1 = [1,2,3,4];
var arr2 = [];
for(var i = 0;i < arr1.length;i++){
arr2[i] = arr1[i];
}
arr2.push(5);
- 继承基本语法
- 继承属性
- function A(){ this.color = "black"; } A.prototype.show = function(){ alert("Hello!"); } function B(){ A.call(this); } var b = new B(); console.log(b.color);//通过callnew出来的b有color属性
- 继承方法
function A(){
this.color = "black";
}
A.prototype.show = function(){
alert("Hello!");
}
function B(){
A.call(this);
}
for(var i in A.prototype){
B.prototype[i] = A.prototype[i];
}
var b = new B();
b.show();
- 拖拽案例
html代码
<div id = "box" style="width: 200px;height: 200px;background-color: red;position: absolute;">
</div>
- 原生js实现拖拽
window.onload = function(){
var box = document.getElementById("box");
box.onmousedown = function(event){//鼠标按下事件
var ev = event||window.event;
var divX = ev.clientX - box.offsetLeft;
var divY = ev.clientY - box.offsetTop;
document.onmousemove = function(event){//鼠标移动事件
var ev = event || window.event;
box.style.left = ev.clientX - divX + "px";
box.style.top = ev.clientY - divY + "px";
}
document.onmouseup = function(){//鼠标抬起事件
document.onmouseup = null;
document.onmousemove = null;
}
}
}
- 面向对象实现拖拽
window.onload = function(){
new Drag("box");
}
function Drag(id){
this.divX = null;
this.divY = null;
this.box = document.getElementById(id);
var that = this;
this.box.onmousedown = function(event){
that.down(event);
}
}
Drag.prototype.down = function(){
var ev = event||window.event;
this.divX = ev.clientX - this.box.offsetLeft;
this.divY = ev.clientY - this.box.offsetTop;
var that = this;
document.onmousemove = function(event){
that.move(event);
}
document.onmouseup = function(){
that.up();
}
}
Drag.prototype.move = function(){
var ev = event||window.event;
this.box.style.left = ev.clientX - this.divX + "px";
this.box.style.top = ev.clientY - this.divY + "px";
}
Drag.prototype.up = function(){
document.onmouseup = null;
document.onmousemove = null;
}
- 继承实现拖拽
html代码
<div id = "box1" style = " width: 200px;height: 200px;background-color: red; position: absolute;">
</div>
<div id = "box2" style = "width: 200px;height: 200px;background-color: red;position: absolute;">
</div>
js代码
window.onload = function(){
new Drag("box1");
new LimitDrag("box2");
}
function Drag(id){
this.divX = null;
this.divY = null;
this.box = document.getElementById(id);
var that = this;
this.box.onmousedown = function(event){
that.down(event);
}
}
Drag.prototype.down = function(){
var ev = event||window.event;
this.divX = ev.clientX - this.box.offsetLeft;
this.divY = ev.clientY - this.box.offsetTop;
var that = this;
document.onmousemove = function(event){
that.move(event);
}
document.onmouseup = function(){
that.up();
}
}
Drag.prototype.move = function(){
var ev = event||window.event;
this.box.style.left = ev.clientX - this.divX + "px";
this.box.style.top = ev.clientY - this.divY + "px";
}
Drag.prototype.up = function(){
document.onmouseup = null;
document.onmousemove = null;
}
function LimitDrag(id){
Drag.call(this,id);
}
for(var i in Drag.prototype){
LimitDrag.prototype[i] = Drag.prototype[i];
}
LimitDrag.prototype.move = function(event){
var ev = event||window.event;
var posX = event.clientX - this.divX;
var posY = event.clientY - this.divY;
if(posX < 0){
posX = 0;
}
if(posX > document.documentElement.clientWidth - this.box.offsetWidth){
posX = document.documentElement.clientWidth - this.box.offsetWidth;
}
if(posY < 0){
posY = 0;
}
if(posY > document.documentElement.clientHeight - this.box.offsetHeight){
posY = document.documentElement.clientHeight - this.box.offsetHeight;
}
this.box.style.left = posX + "px";
this.box.style.top = posY + "px";
}
5. 原型链

JavaScript 高级之面向对象的更多相关文章
- javascript高级特性(面向对象)
javascript高级特性(面向对象): * 面向对象: * 面向对象和面向过程的区别: * 面向对象:人就是对象,年龄\性别就是属性,出生\上学\结婚就是方法. * 面向过程:人出生.上学.工作. ...
- Javascript高级程序设计——面向对象小结
ECMAScript支持面向对象编程,对象可以在代码执行时创建,具有动态扩展性而非严格意义上的实体. 创建对象方法: 工厂模式:简单的函数创建引用类型 构造函数模式:可以创建自定义引用类型,可以想创建 ...
- Javascript高级程序设计——面向对象之理解对象
在面向对象语言中都有类的概念,通过类来创建具有属性和方法的对象.而ECMAScript中没有类的概念,ECMAScript中定义了对象:无需属性的集合,其属性值可以包含基本值.对象.或者函数. 在Ja ...
- JavaScript高级与面向对象
对象:任何事物都可以看作是对象. 1.面向对象与面向过程的概念 面向过程:凡是自己亲力亲为,自己按部就班的解决现有问题. 面向对象:自己充当一个指挥者的角色,指挥更加专业的对象帮我解决问题. 联系:面 ...
- 2020/06/06 JavaScript高级程序设计 面向对象的程序设计
ECMAScript虽然是一种面向对象的语言,但是他没有类的概念.所以他的对象也与其他语言中的对象有所不同. ECMA-262定义对象:一组没有特定顺序的值. 6.1 理解对象 创建对象的方法: 1. ...
- Javascript高级程序设计——面向对象之实现继承
原型链: 构造函数中都有一个prototype属性指针,这个指针指向原型对象,而创建的实例也有指向这个原型对象的指针__proto__.当实例查找方法时先在实例上找,找不到再通过__proto__到原 ...
- Javascript高级程序设计——面向对象之创建对象
对象创建方法: 工厂方法 构造函数模式 原型模式 组合构造函数和原型模式 寄生构造函数模式 问题构造函数模式 工厂模式: function Person(name, age){ var obj = n ...
- Javascript高级篇-面向对象的特性
一.创建对象 1.1初始化器 var any={ name:"some", age:10, action:function(){ alert(this.name+":&q ...
- javascript高级特性
01_javascript相关内容02_函数_Arguments对象03_函数_变量的作用域04_函数_特殊函数05_闭包_作用域链&闭包06_闭包_循环中的闭包07_对象_定义普通对象08_ ...
随机推荐
- 依赖项从GIT上拉下来出现黄色三角形解决方法
1.进入程序包管理器设置 2.添加程序包源=>输入名称(自己定\默认)=>NuGet源地址https://nuget.org/api/v2/(2019年1月30号可用)更新确认后重生项目即 ...
- oracle中查看一张表是否有主键,主键在哪个字段上
利用Oracle中系统自带的两个视图可以实现查看表中主键信息,语句如下:select a.constraint_name, a.column_name from user_cons_columns a ...
- 免费申请 WebStorm 使用许可 - free JetBrains Open Source license(s)
闲聊 步入前端切图仔行列的我曾多次纠结过「到底使用哪种编辑器写前端好用?」这样的问题,前前后后尝试过 Dreamweaver .HBuilder .Sublime Text .Atom 和现在主要使用 ...
- React学习(一)
一. 允许HTML和JavaScript代码混写,使用JSX语法:遇到HTML标签就用HTML规则解析,遇到{}的代码块就用js解析 var names = ['Alice', 'Emily', 'K ...
- 课时9.HTML发展史(了解)
这个图片里的时间不用都记住,只需要记住一些特殊的,1993年,1995年(在W3C接手以后,才有了真正意义上的标准),1999年这几个时间 WHATWG的目的是推广HTML的标准,HTML5是浏览器厂 ...
- 课时17.WebStorm安装(理解)
开发工具(工欲善其事,必先利其器) 为了让大家更快的融入到编程的世界中去,不被繁琐的英文单词所困扰,不用每天编写很多没有意义的重复代码,提升大家的开发效率,今后的课程中我们统一采用开发工具来编写网页 ...
- Vue2 轮播图组件 slide组件
Vue2原生始轮播图组件,支持宽度自适应.高度设置.轮播时间设置.左右箭头按钮控制,圆点按钮切换,以及箭头.圆点按钮是否显示. <v-carousel :slideData="slid ...
- 一种比使用协程更方便的方法:Invoke(),同样达到等待执行的效果
1.Invoke(string methodName,float time) 在一定时间调用methodName函数 using UnityEngine; using System.Collectio ...
- 【python安装】Windows上安装和创建python开发环境
1. 在 windows10 上安装python开发环境 Linux和Mac OS都自带python环境,但是Windows没有,所以需要自行安装. 第1步:访问 python官网,下载Windows ...
- PHP-学习笔记-进阶
PHP-学习笔记-进阶 PHP类和对象之定义类的方法 访问控制的关键字代表的意义为: public:公开的 protected:受保护的 private:私有的 我们可以这样定义方法: class C ...