什麼是對象面試
屬性組成數組
屬性的分類:bash
特別的對象函數
對象是一種複合數據類型,能夠保存不一樣類型的屬性;ui
建立對象this
var obj = new object();
複製代碼
向對象中添加屬性spa
obj.屬性名 = 屬性值;
obj[‘屬性名’] = 屬性值;
複製代碼
使用[]去操做屬性時,[]中傳遞的是一個字符串
。能傳字符串的地方就能傳變量
;var p = {
name: 'Tom',
age: 23,
setName: function (name) {
this.name = name
}
}
console.log(p.name, p.age)
p.setName('JACK')
console.log(p.name, p.age)
var p2 = {
name: 'BOB',
age: 24,
setName: function (name) {
this.name = name
}
}
複製代碼
// 一我的: name:"Tom", age: 12
var p = new Object()
p = {}
p.name = 'Tom'
p.age = 12
p.setName = function (name) {
this.name = name
}
p.setaAge = function (age) {
this.age = age
}
console.log(p)
複製代碼
// 工廠函數: 返回一個須要的數據的函數
function createPerson(name, age) {
var p = {
name: name,
age: age,
setName: function (name) {
this.name = name
}
}
return p
}
var p1 = createPerson('Tom', 12)
var p2 = createPerson('JAck', 13)
console.log(p1)
console.log(p2)
複製代碼
function Person(name, age) {
this.name = name
this.age = age
this.setName = function (name) {
this.name = name
}
}
var p1 = new Person('Tom', 12)
var p2 = new Person('Tom2', 13)
console.log(p1, p1 instanceof Person)
複製代碼
一、prototype本質上仍是一個JavaScript對象; 二、每一個函數都有一個默認的prototype屬性; 三、經過prototype咱們能夠擴展Javascript的內建對象prototype
// 構造函數
function Base(){}
var baseObj = new Base()
複製代碼
var obj = {};
obj.__proto__ = Base.prototype;
Base.call(obj);
複製代碼
let newObj = function(func){
//建立對象,錯誤示範:Object.create()方法建立一個新對象,使用現有的對象的prototype指向括號中的對象func.prototype。
// let obj = Object.create(func.prototype)
// 因此應該以下創造對象,是爲了使新建立的對象的__proto__指向構造函數的原型func.prototype
let obj = new Object()
obj.__proto__=func.prototype
// 將構造函數的做用域給新的對象,而且執行構造函數
// 若是構造函數有返回值,那就返回返回值,若是沒有,會返回undefined
let k = func.call(obj)
if(typeof k === 'object'){
// 若是返回的類型是一個對象,那就返回該對象
return k
}else{
// 若是構造函數執行後,返回的類型不是一個對象的話,那就返回建立的對象
return obj
}
}
複製代碼
// 建立父對象
var parentObj = {
name: 'parentName',
age: 25,
showName:function(){
console.log(this.name);
}
}
// 建立須要繼承的子對象
var childrenObj= {}
// 開始拷貝屬性(使用for...in...循環)
for(var i in parentObj){
childrenObj[i] = parentObj[i]
}
console.log(childrenObj); //{ name: 'parentName', age: 25, showName: [Function: showName] }
console.log(parentObj); // { name: 'parentName', age: 25, showName: [Function: showName] }
複製代碼
// 建立父構造函數
function Parent(){}
// 設置父構造函數的原型對象
Parent.prototype.age = 25;
Parent.prototype.friends = ['小名','小麗'];
Parent.prototype.showAge = function(){
console.log(this.age);
};
// 建立子構造函數
function Child(){}
// 設置子構造器的原型對象實現繼承
Child.prototype = Parent.prototype
// 由於子構造函數的原型被覆蓋了, 因此如今子構造函數的原型的構造器屬性已經再也不指向Child,而是Parent。此時實例化Child和實例化parent的區別是不大的,因此再次建立Child是沒有意義的,而且Child.prototype添加屬性,也是會影響到Parent.prototype;
console.log(Child.prototype.constructor == Parent);// true
console.log(Parent.prototype.constructor == Parent);// true
// 問題就在這裏!!!!
// 因此咱們須要修正一下
Parent.prototype.constructor = Child;
// 上面這行代碼以後, 就實現了繼承
var childObj = new Child();
console.log(childObj.age);// 25
console.log(childObj.friends);// ['小名','小麗']
childObj.showAge();// 25
複製代碼
// 定義父構造函數
function Parent(name,friends){
this.name = name;
this.friends = friends;
}
Parent.prototype.test = function(){
console.log('原型方法', this.friends)
};
// 定義子構造函數
function Child(name,friends,age){
this.age = '12'
}
// 將子構造函數的原型指定父函數的實例
Child.prototype = new Parent('parentName',['a','b','c']);
// 可是
console.log(Child.prototype.constructor);
//輸出:function Parent(){this.name = 'me';this.sex = ['male','female']}
// 因此,把Child的原型的構造函數修復爲child
Child.prototype.constructor = Child
var childObj = new Child('childName',[3,4,'ddd'],24);//有test()
// 問題一:子實例沒法向父類傳值
console.log(childObj.name,childObj.friends) // parentName和["a", "b", "c"]
// 問題二:若是其中一個子類修改了父類中的引用數據類型的屬性,那麼就會影響其餘的子類
var childObj2 = new Child('childName',[3,4],24);
childObj2.friends.push('additem')
console.log(childObj1.friends,childObj2.friends)// ["a", "b", "c", "additem"], ["a", "b", "c", "additem"]
複製代碼
function Parent(xxx){this.xxx = xxx}
Parent.prototype.test = function(){};
function Child(xxx,yyy){
Parent.call(this, xxx);
}
var child = new Child('a', 'b'); //child.xxx爲'a', 但child沒有test()
// 問題:
console.log(child.test);// undefined
複製代碼
// 建立父構造函數
// 父類屬性
function Parent(name){
this.name = name;
this.sex = ['male','female']
}
// 父類原型方法
Parent.prototype.test = function(){
console.log(this.name)
};
// 定義子構造函數
function Child(name,age){
// 複製父級構造函數的屬性和方法
// 使得每個子對象都能複製一份父對象的屬性且不會相互影響
Parent.call(this,name);//繼承實例屬性,第一次調用Parent()
this.age = age
}
// 將子構造函數的原型對象指向父級實例
var parentObj = new Parent();//繼承父類方法,第二次調用Parent()
Child.prototype = parentObj; //獲得test()
// 將子構造函數Child原型的構造函數修復爲Child
Child.prototype.constructor = Child;
var childObj = new Child('zhangsan',15); console.log(childObj,childObj.name,childObj.sex,childObj.test)
// 輸出:childObj.name:'zhangsan';childObj.sex:["male", "female"];childObj.test:一個函數
複製代碼
// 建立父構造函數
function Parent(){
this.name = 'me';
this.sex = ['male','female']
}
Parent.prototype.test = function(){};
// 定義子構造函數
function Child(){
// 複製父級構造函數的屬性和方法
// 使得每個子對象都能複製一份父對象的屬性且不會相互影響
Parent.call(this);
this.age = '12'
}
// 定義空函數
function F(){}
// 把空函數的原型指向Parent.prototype
// 寄生式組合繼承
F.prototype = Parent.prototype
// 將子構造函數的原型對象指向空函數F的實例對象fObj
var fObj = new F();
Child.prototype = fObj;
// 將子構造函數Child原型的構造函數修復爲Child
Child.prototype.constructor = Child;
var childObj = new Child();
複製代碼