function Person(name,age,job){
this.name=name;
this.age=age;
this.job=job;
}
Person.prototype.sayName=function(){
alert(this.name);
}
var person1 = new Person("张三","29","web frontpage manager");
var person2 = new Person("李四","22","doctor");
person1.sayName(); //弹出"张三"
console.log(person2.name)//输出“李四”
var myMammal = {
name : 'Herb the Mammal',
get_name : function () {
return this.name;
},
says : function () {
return this.saying || '';
}
}
var myCat = Object.create(myMammal);
myCat.name = 'Henrietta';
myCat.saying = 'meow';
myCat.get_name = function () {
console.log(this.says + ' ' + this.name + this.says);
}
myCat.get_name();
function () {
return this.saying || '';
} Henriettafunction () {
return this.saying || '';
}
if (!Object.create) {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
var Dog= {
creatFn: function(){
// some code here
}
};
var Dog= {
creatFn: function(){
var dog= {};
dog.name = "狗狗";
dog.makeSound = function(){ alert("汪汪汪"); };
return dog;
}
};
var dog1 = Dog.creatFn(); dog1.makeSound(); // 汪汪汪
var Animal = {
creatFn: function(){
var animal = {};
animal.eat= function(){ alert("吃饭饭"); };
return animal;
}
};
var Dog= {
creatFn: function(){
var dog= Animal.creatFn();
dog.name = "狗狗";
dog.makeSound = function(){ alert("汪汪汪"); };
return dog;
}
};
var dog1= Dog.creatFn(); dog1.eat(); // 吃饭饭
var Dog= {
creatFn: function(){
var dog= {};
var sound = "汪汪汪";
dog.makeSound = function(){ alert(sound); };
return dog;
}
};
var dog1 = Dog.creatFn(); alert(dog1.sound); // undefined
var Dog= {
sound : "汪汪汪",
creatFn: function(){
var dog= {};
dog.makeSound = function(){ alert(Dog.sound); };
dog.changeSound = function(x){ Dog.sound = x; };
return dog;
}
};
var dog1 = Dog.creatFn(); var dog2 = Dog.creatFn(); dog1.makeSound(); // 汪汪汪
dog2.changeSound("呜呜呜");
dog1.makeSound(); //呜呜呜
function Parent(){
this.name = 'mike';
}
function Child(){
this.age = 12;
}
Child.prototype = new Parent();//Child继承Parent,通过原型,形成链条
var test = new Child();
alert(test.age);
alert(test.name);//得到被继承的属性
//继续原型链继承
function Brother(){ //brother构造
this.weight = 60;
}
Brother.prototype = new Child();//继续原型链继承
var brother = new Brother();
alert(brother.name);//继承了Parent和Child,弹出mike
alert(brother.age);//弹出12
function Parent(age){
this.name = ['mike','jack','smith'];
this.age = age;
}
function Child(age){
Parent.call(this,age);
}
var test = new Child(21);
alert(test.age);//21
alert(test.name);//mike,jack,smith
test.name.push('bill');
alert(test.name);//mike,jack,smith,bill
function Parent(age){
this.name = ['mike','jack','smith'];
this.age = age;
}
Parent.prototype.run = function () {
return this.name + ' are both' + this.age;
};
function Child(age){
Parent.call(this,age);//对象冒充,给超类型传参
}
Child.prototype = new Parent();//原型链继承
var test = new Child(21);//写new Parent(21)也行
alert(test.run());//mike,jack,smith are both21
function obj(o){
function F(){}
F.prototype = o;
return new F();
}
function obj(o){
function F(){}
F.prototype = o;
return new F();
}
var box = {
name : 'trigkit4',
arr : ['brother','sister','baba']
};
var b1 = obj(box);
alert(b1.name);//trigkit4
b1.name = 'mike';
alert(b1.name);//mike
alert(b1.arr);//brother,sister,baba
b1.arr.push('parents');
alert(b1.arr);//brother,sister,baba,parents
var b2 = obj(box);
alert(b2.name);//trigkit4
alert(b2.arr);//brother,sister,baba,parents
function creatAnother(original){
var clone=new Object(original);
clone.sayHi=function(){
alert("hi")
};
return clone;
}
var person={
name:"haorooms",
friends:["hao123","zhansan","lisi"]
}
var antherPerson=creatAnother(person);
antherPerson.sayHi();//hi
function inheritPrototype (subType,superType) {
var prototype = Object.creat(superType.prototype);
prototype.constructor = subType;
subType.prototype = prototype;
};
function SuperType (name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
SuperType.prototype.sayName = function () {
console.log(this.name);
}
function SubType(name, age) {
//继承属性
SuperType.call(this,name);
this.age = age;
}
//继承方法
inheritPrototype(SubType,SuperType);
SubType.prototype.sayAge = function () {
console.log(this.age);
}
var instance = new SubType();
//定义类
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return '('+this.x+', '+this.y+')';
}
}
var point = new Point(2, 3);
point.toString() // (2, 3)
point.hasOwnProperty('x') // true
point.hasOwnProperty('y') // true
point.hasOwnProperty('toString') // false
point.__proto__.hasOwnProperty('toString') // true
class ColorPoint extends Point {
constructor(x, y, color) {
super(x, y); // 调用父类的constructor(x, y)
this.color = color;
}
toString() {
return this.color + ' ' + super.toString(); // 调用父类的toString()
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有