對象、原型

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()

【個人總結,如有錯漏,歡迎指出】
:>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容