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 高级之面向对象的更多相关文章

  1. javascript高级特性(面向对象)

    javascript高级特性(面向对象): * 面向对象: * 面向对象和面向过程的区别: * 面向对象:人就是对象,年龄\性别就是属性,出生\上学\结婚就是方法. * 面向过程:人出生.上学.工作. ...

  2. Javascript高级程序设计——面向对象小结

    ECMAScript支持面向对象编程,对象可以在代码执行时创建,具有动态扩展性而非严格意义上的实体. 创建对象方法: 工厂模式:简单的函数创建引用类型 构造函数模式:可以创建自定义引用类型,可以想创建 ...

  3. Javascript高级程序设计——面向对象之理解对象

    在面向对象语言中都有类的概念,通过类来创建具有属性和方法的对象.而ECMAScript中没有类的概念,ECMAScript中定义了对象:无需属性的集合,其属性值可以包含基本值.对象.或者函数. 在Ja ...

  4. JavaScript高级与面向对象

    对象:任何事物都可以看作是对象. 1.面向对象与面向过程的概念 面向过程:凡是自己亲力亲为,自己按部就班的解决现有问题. 面向对象:自己充当一个指挥者的角色,指挥更加专业的对象帮我解决问题. 联系:面 ...

  5. 2020/06/06 JavaScript高级程序设计 面向对象的程序设计

    ECMAScript虽然是一种面向对象的语言,但是他没有类的概念.所以他的对象也与其他语言中的对象有所不同. ECMA-262定义对象:一组没有特定顺序的值. 6.1 理解对象 创建对象的方法: 1. ...

  6. Javascript高级程序设计——面向对象之实现继承

    原型链: 构造函数中都有一个prototype属性指针,这个指针指向原型对象,而创建的实例也有指向这个原型对象的指针__proto__.当实例查找方法时先在实例上找,找不到再通过__proto__到原 ...

  7. Javascript高级程序设计——面向对象之创建对象

    对象创建方法: 工厂方法 构造函数模式 原型模式 组合构造函数和原型模式 寄生构造函数模式 问题构造函数模式 工厂模式: function Person(name, age){ var obj = n ...

  8. Javascript高级篇-面向对象的特性

    一.创建对象 1.1初始化器 var any={ name:"some", age:10, action:function(){ alert(this.name+":&q ...

  9. javascript高级特性

    01_javascript相关内容02_函数_Arguments对象03_函数_变量的作用域04_函数_特殊函数05_闭包_作用域链&闭包06_闭包_循环中的闭包07_对象_定义普通对象08_ ...

随机推荐

  1. 【腾讯敏捷转型No.6】如何打造称手的敏捷工具

    通常情况下,大家对于敏捷的感受就是:大家一起来开站立晨会啦!然后一大早,大家拿着早餐,围成一个圈,听一个人在讲话. 在很多公司,决定采用敏捷之后,都会从晨会开始,因为很多人觉得敏捷其它模块都很难学习, ...

  2. #leetcode刷题之路23-合并K个排序链表

    合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例:输入:[ 1->4->5, 1->3->4, 2->6]输出: 1->1->2- ...

  3. golang总结-Redis整合

    目录 1. 基本用法 2. Redis连接池 go get github.com/gomodule/redigo/redis 1. 基本用法 获取连接 package conn import ( &q ...

  4. lnmp+coreseek实现站内全文检索(安装篇)

    coreseek安装与简单实用 安装环境 系统环境 centos7.2 1核2G 软件环境 coreseek-3.2.14 lnmp1.5 安装mmseg 更新依赖包和安装编译环境 yum -y in ...

  5. CDH升级 5.7.5 --> 5.13.3(tar包方式)

    博客园首发,转载请注明出处:https://www.cnblogs.com/tzxxh/p/9123231.html 一.准备 1.关闭cdh中的服务 hdfs.yarn等所有服务:关闭 cm-ser ...

  6. 发现环——第八届蓝桥杯C语言B组(国赛)第四题

    原创 标题:发现环 小明的实验室有N台电脑,编号1~N.原本这N台电脑之间有N-1条数据链接相连,恰好构成一个树形网络.在树形网络上,任意两台 电脑之间有唯一的路径相连. 不过在最近一次维护网络时,管 ...

  7. C语言中赋值表达式的返回值是什么?

    我们或多或少都有过,或者见过将赋值表达式参与运算的情况.这通常会伴随着一些意想不到的问题.今天我就见到了一段奇怪的代码: #include<stdio.h> int main() { ; ...

  8. java int 与 Integer之间的区别

    int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型 ...

  9. golang 防SQL注入 基于反射、TAG标记实现的不定参数检查器

    收到一个任务,所有http的handler要对入参检查,防止SQL注入.刚开始笨笨的,打算为所有的结构体写一个方法,后来统计了下,要写几十上百,随着业务增加,以后还会重复这个无脑力的机械劳作.想想就l ...

  10. Binary Indexed Tree (Fenwick Tree)

    Binary Indexed Tree 主要是为了存储数组前缀或或后缀和,以便计算任意一段的和.其优势在于可以常数时间处理更新(如果不需要更新直接用一个数组存储所有前缀/后缀和即可).空间复杂度O(n ...