function user(name,age,gender ) {
var person= {};
person. name =name;
person.age = age;
person.gender = gender;
return person;
}
var whh= user("王花花",18,"male")
console.log(whh);
工厂函数,用作生成对象
function User(name,age,gender) {
this.name=name;
this.age= age;
this.gender=gender;
}
var whh2= new User("王花花2",28,"female")
console.log(whh2)
- 构造函数
- 没有return关键词 必须用new
- 一般首字母会大写
var a = {};
console.log(a);
点开后_proto_:下constructor:f Object()
即
var a = {}
var a = new Object ()
两者相等
var a = Object.creat(null)
创建不带任何继承对象的方法
var a = Object.creat(
a=1,
b=2
)
同样也可以使用这个方法自定义继承
function 原型(属性1,属性2){
this.属性1=属性1;
this.属性2=属性2;
}
//上面为原型
function 继承1(){}
继承1.prototype = Object.creat( 原型.prototype);
继承1.prototype.功能1 = console. log("功能1");c
//用Object.creat 方法创建继承链
下面是某个实例
//动物
function Animal(color, weight) {
this.color = color;
this.weight = weight
}
Animal.prototype.eat = function () {
console.log("吃东西");
}
Animal.prototype.sleep = function () {
console.log("睡觉");
};
//哺乳动物
function Mammal(color, weight) {
Animal.call(this,color,weight)
}
Mammal.prototype = Object
.create(Animal.prototype);
Mammal.prototype.constructor = Mammal
Mammal.prototype.suckle = function () {
console.log("吃奶");
}
var b = new Mammal();
//人类
function Person(color,weight) {
mammal.call(this,color,weight)
}
Person.prototype = Object.create(Mammal.prototype);
// Person.prototype.constructor = Person
Person.prototype.lie = function(){
console.log("说谎");
}
var c = new Person()
涉及到了一些关于call this 的知识,以后的博客中会补全