对象、原型

1.OOP 指什么?有哪些特性

OOP是指面向对象编程Object-oriented programming。面向对象中最重要的是类和对象。类是具备了某些功能和属性的抽象模型。而类是实例化之后就是对象。

特性:
1、继承性
2、封装性:将一个类的实现和使用分开,只保留部分接口与外部联系
3、多态性:子类继承了来自父级类中的属性和方法,可以对其中方法进行重写。

2.如何通过构造函数的方式创建一个拥有属性和方法的对象?

function People(name,age){
    this.name = name;
    this.age = age;
    }
}

People.prototype.sayName = function(){
    console.log('name:'+ this.name)
}

var p1 = new People('jack','20')

p1.sayName();//name:jack

3. prototype 是什么?有什么特性

prototype是显示原型对象,每一个函数对象都有prototype属性,指向另一个对象。这个对象的所有属性和方法都会被构造的实例继承。

4.画出如下代码的原型图

function People (name){
  this.name = name;
  this.sayName = function(){
    console.log('my name is:' + this.name);
  }
}

People.prototype.walk = function(){
  console.log(this.name + ' is walking');  
}

var p1 = new People('小八');
var p2 = new People('前端');
原型图.png

5.创建一个 Car 对象,拥有属性name、color、status;拥有方法run,stop,getStatus

function Car(name,color,status){
    this.name = name;
    this.color = color;
    this.status = status;
}

Car.prototype.run = function(){
    console.log('run')
}
Car.prototype.stop = function(){
    console.log('stop')
}
Car.prototype.getStatus = function(){
    console.log(this.status);
}

var myCar = new Car('jack','blue','running')

6.创建一个 GoTop 对象,当 new 一个 GotTop 对象则会在页面上创建一个回到顶部的元素,点击页面滚动到顶部。拥有以下属性和方法

1. `ct`属性,GoTop 对应的 DOM 元素的容器
2.  `target`属性, GoTop 对应的 DOM 元素
3.  `bindEvent` 方法, 用于绑定事件
4. `createNode` 方法, 用于在容器内创建节点

function GoTop(){
    this.ct = $('.ct');
    this.target = $('<p class="gotop">回到顶部<p>');
    this.creaNode()
    this.bindEvent()
}

GoTop.prototype = {
    bindEvent:function(){
        this.target.on('click',function(){
            $(window).scrollTop(0)
        })
        var _this = this
        $(window).on('scrollTop',function(){
            if($(this).scrollTop()>500){
                _this.target.show()
            }else{
                _this.target.hide()
            }
        })
    }
    createNode:function(){
        this.ct.append(this.target)
    }
}
var go1 = new GoTop()
go1.bindEvent()
go1.createNode()

【个人总结,如有错漏,欢迎指出】
:>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容