什么是对象面试
属性组成数组
属性的分类: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();
复制代码