var person = new Object();
person.name = "Rose";
person.age = 18;
person.getName = function () {
return this.name;
};
console.log(person.name);//Rose
console.log(person.getName);//function () {return this.name;}
console.log(person.getName());//Rose
var person = {
name : "Rose",
age : 18 ,
getName : function () {
return this.name;
}
};
console.log(person.name);//Rose
console.log(person.getName);//function () {return this.name;}
console.log(person.getName());//Rose
function createPerson(name,age) {
var object = new Object();
object.name = name;
object.age = age;
object.getName = function () {
return this.name;
};
return object;
}
var person1 = createPerson("Rose",18);
var person2 = createPerson("Jack",20);
console.log(person1.name);//Rose
console.log(person2.name);//Jack
console.log(person1.getName === person2.getName);//false//重复生成函数,为每个对象都创建独立的函数版本
function createPerson(name,age) {
var object = new Object();
object.name = name;
object.age = age;
object.getName = getName;
return object;
}
function getName() {
return this.name;
}
var person1 = createPerson("Rose",18);
var person2 = createPerson("Jack",20);
console.log(person1.name);//Rose
console.log(person2.name);//Jack
console.log(person1.getName === person2.getName);//true//共享同一个函数
function Person(name,age) {
this.name = name;
this.age = age;
this.getName = function () {
return this.name;
}
}
var person1 = new Person("Rose",18);
var person2 = new Person("Jack",20);
console.log(person1.name);//Rose
console.log(person2.name);//Jack
console.log(person1.getName === person2.getName); //false//重复生成函数,为每个对象都创建独立的函数版本
function Person(name,age) {
this.name = name;
this.age = age;
this.getName = getName ;
}
function getName() {
return this.name;
}
var person1 = new Person("Rose",18);
var person2 = new Person("Jack",20);
console.log(person1.name);//Rose
console.log(person2.name);//Jack
console.log(person1.getName === person2.getName); //true//共享同一个函数
function Person() {
}
Person.prototype.name = 'Rose';
Person.prototype.age = 18;
Person.prototype.getName = function () {
return this.name;
};
var person1 = new Person();
var person2 = new Person();
console.log(person1.name);//Rose
console.log(person2.name);//Rose//共享同一个属性
console.log(person1.getName === person2.getName);//true//共享同一个函数
function Person() {
}
Person.prototype.name = 'Rose';
Person.prototype.age = 18;
Person.prototype.lessons = ["语文","数学"];
Person.prototype.getName = function () {
return this.name;
};
var person1 = new Person();
person1.lessons.push("英语");
var person2 = new Person();
console.log(person1.lessons);//["语文", "数学", "英语"]
console.log(person2.lessons);//["语文", "数学", "英语"]//person1修改影响了person2
function Person(name,age) {
this.name = name;
this.age = age;
}
Person.prototype.getName = function () {
return this.name;
};
var person1 = new Person('Rose', 18);
var person2 = new Person('Jack', 20);
console.log(person1.name);//Rose
console.log(person2.name);//Jack
console.log(person1.getName === person2.getName);//true//共享原型中定义的方法
function Person(name,age) {
this.name = name;
this.age = age;
if (typeof Person._getName === "undefined"){
Person.prototype.getName = function () {
return this.name;
};
Person._getName = true;
}
}
var person1 = new Person('Rose', 18);
var person2 = new Person('Jack', 20);
console.log(person1.name);//Rose
console.log(person2.name);//Jack
console.log(person1.getName === person2.getName);//true//共享原型中定义的方法
function Person(name,age) {
this.name = name;
this.age = age;
if (typeof this.getName !== "function"){
Person.prototype.getName = function () {
return this.name;
};
}
}
var person1 = new Person('Rose', 18);
var person2 = new Person('Jack', 20);
console.log(person1.name);//Rose
console.log(person2.name);//Jack
console.log(person1.getName === person2.getName);//true//共享原型中定义的方法
function Person(name,age) {
this.name = name;
this.age = age;
if (typeof Person._getName === "undefined"){
Person.prototype.getName = function () {
return this.name;
};
Person._getName = true;
}
}
var person = new Person("Rose",18);
person.job = 'Engineer';//添加属性
console.log(person.job);//Engineer
delete person.job;//删除属性
console.log(person.job);//undefined//删除属性后值为undefined
person.age = undefined;//删除属性
console.log(person.age);//undefined//删除属性后值为undefined
function Person(name,age) {
this.name = name;
this.age = age;
if (typeof Person._getName === "undefined"){
Person.prototype.getName = function () {
return this.name;
};
Person._getName = true;
}
}
var person = new Person("Rose",18);
Object.defineProperty(person,"name",{configurable:false,writable:false});
person.name = "Jack";
console.log(person.name);//Rose//重新赋值无效
delete person.name;
console.log(person.name);//Rose//删除无效
function Person(name,age) {
this.name = name;
this.age = age;
if (typeof Person._getName === "undefined"){
Person.prototype.getName = function () {
return this.name;
};
Person._getName = true;
}
}
var person = new Person("Rose",18);
Object.defineProperty(person,"name",{configurable:false,writable:false});
person.name = "Jack";
console.log(person.name);//Rose//重新赋值无效
delete person.name;
console.log(person.name);//Rose//删除无效
Object.defineProperty(person,"name",{configurable:true,writable:true});//Cannot redefine property: name
function Person(name,age) {
this.name = name;
this._age = age;
if (typeof Person._getName === "undefined"){
Person.prototype.getName = function () {
return this.name;
};
Person._getName = true;
}
}
var person = new Person("Rose",18);
Object.defineProperty(person,"age",{
get:function () {
return this._age;
},
set:function (age) {
this._age = age;
}});
person.age = 20;
console.log(person.age);//20//person.age=20是使用set方法将20赋值给_age,person.age是使用get方法将_age的读取出来
console.log(person._age);//20
function Person(name,age) {
this.name = name;
this._age = age;
if (typeof Person._getName === "undefined"){
Person.prototype.getName = function () {
return this.name;
};
Person._getName = true;
}
}
var person = new Person("Rose",18);
Object.defineProperty(person,"age",{
get:function () {
return this._age;
},
set:function (age) {
this._age = age;
}});
console.log(Object.getOwnPropertyNames(person));//["name", "_age", "age"]
console.log(Object.getOwnPropertyDescriptor(person,"age"));//{enumerable: false, configurable: false, get: function, set: function}
function Father(name) {
this.name = name ;
this.getName = function () {
return this.name;
}
}
function Son(name,age) {
this._newMethod = Father;
this._newMethod(name);
delete this._newMethod;
this.age = age;
this.getAge = function () {
return this.age;
}
}
var father = new Father("Tom");
var son = new Son("Jack",18);
console.log(father.getName());//Tom
console.log(son.getName());//Jack//继承父类getName()方法
console.log(son.getAge());//18
function FatherA(name) {
this.name = name ;
this.getName = function () {
return this.name;
}
}
function FatherB(job) {
this.job = job;
this.getJob = function () {
return this.job;
}
}
function Son(name,job,age) {
this._newMethod = FatherA;
this._newMethod(name);
delete this._newMethod;
this._newMethod = FatherB;
this._newMethod(job);
delete this._newMethod;
this.age = age;
this.getAge = function () {
return this.age;
}
}
var fatherA = new FatherA("Tom");
var fatherB = new FatherB("Engineer");
var son = new Son("Jack","Programmer",18);
console.log(fatherA.getName());//Tom
console.log(fatherB.getJob());//Engineer
console.log(son.getName());//Jack//继承父类FatherA的getName()方法
console.log(son.getJob());//Programmer//继承父类FatherB的getJob()方法
console.log(son.getAge());//18
function Father(name) {
this.name = name ;
this.getName = function () {
return this.name;
}
}
function Son(name,job,age) {
Father.call(this,name);
this.age = age;
this.getAge = function () {
return this.age;
}
}
var father = new Father("Tom");
var son = new Son("Jack","Programmer",18);
console.log(father.getName());//Tom
console.log(son.getName());//Jack//继承父类getName()方法
console.log(son.getAge());//18
function FatherA(name) {
this.name = name ;
this.getName = function () {
return this.name;
}
}
function FatherB(job) {
this.job = job;
this.getJob = function () {
return this.job;
}
}
function Son(name,job,age) {
FatherA.call(this,name);
FatherB.call(this,job);
this.age = age;
this.getAge = function () {
return this.age;
}
}
var fatherA = new FatherA("Tom");
var fatherB = new FatherB("Engineer");
var son = new Son("Jack","Programmer",18);
console.log(fatherA.getName());//Tom
console.log(fatherB.getJob());//Engineer
console.log(son.getName());//Jack//继承父类FatherA的getName()方法
console.log(son.getJob());//Programmer//继承父类FatherB的getJob()方法
console.log(son.getAge());//18
function Father(name) {
this.name = name ;
this.getName = function () {
return this.name;
}
}
function Son(name,job,age) {
Father.apply(this,new Array(name));
this.age = age;
this.getAge = function () {
return this.age;
}
}
var father = new Father("Tom");
var son = new Son("Jack","Programmer",18);
console.log(father.getName());//Tom
console.log(son.getName());//Jack//继承父类getName()方法
console.log(son.getAge());//18
function FatherA(name) {
this.name = name ;
this.getName = function () {
return this.name;
}
}
function FatherB(job) {
this.job = job;
this.getJob = function () {
return this.job;
}
}
function Son(name,job,age) {
FatherA.apply(this,new Array(name));
FatherB.apply(this,new Array(job));
this.age = age;
this.getAge = function () {
return this.age;
}
}
var fatherA = new FatherA("Tom");
var fatherB = new FatherB("Engineer");
var son = new Son("Jack","Programmer",18);
console.log(fatherA.getName());//Tom
console.log(fatherB.getJob());//Engineer
console.log(son.getName());//Jack//继承父类FatherA的getName()方法
console.log(son.getJob());//Programmer//继承父类FatherB的getJob()方法
console.log(son.getAge());//18
function Father() {
}
Father.prototype.name = "Tom";
Father.prototype.getName = function () {
return this.name;
};
function Son() {
}
Son.prototype = new Father();
Son.prototype.age = 18;
Son.prototype.getAge = function () {
return this.age;
};
var father = new Father();
var son = new Son();
console.log(father.getName());//Tom
console.log(son.getName());//Tom//继承父类FatherA的getName()方法
console.log(son.getAge());//18
function Father(name) {
this.name = name;
}
Father.prototype.getName = function () {
return this.name;
};
function Son(name,age) {
Father.call(this,name);
this.age = age;
}
Son.prototype = new Father();
Son.prototype.getAge = function () {
return this.age;
};
var father = new Father("Tom");
var son = new Son("Jack",18);
console.log(father.getName());//Tom
console.log(son.getName());//Jack//继承父类Father的getName()方法
console.log(son.getAge());//18
function Person(name) {
this.name = name;
if (typeof this.getName !== "function"){
Person.prototype.getName = function () {
return this.name;
}
}
if (typeof this.toEat !== "function"){
Person.prototype.toEat = function (animal) {
console.log( this.getName() + "说去吃饭:");
animal.eat();
}
}
}
function Animal(name) {
this.name = name;
if (typeof this.getName !== "function"){
Animal.prototype.getName = function () {
return this.name;
}
}
}
function Cat(name) {
Animal.call(this,name);
if (typeof this.eat !== "function"){
Cat.prototype.eat = function () {
console.log(this.getName() + "吃鱼");
}
}
}
Cat.prototype = new Animal();
function Dog(name) {
Animal.call(this,name);
if (typeof this.eat !== "function"){
Dog.prototype.eat = function () {
console.log(this.getName() + "啃骨头");
}
}
}
Dog.prototype = new Animal();
var person = new Person("Tom");
person.toEat(new Cat("cat"));//Tom说去吃饭:cat吃鱼
person.toEat(new Dog("dog"));//Tom说去吃饭:dog啃骨头
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有