function A(){
}
A.prototype = {
aa:"aa",
method:function(){
}
};
var a = new A;
var b = new A;
console.log(a.aa === b.aa);
console.log(a.method === b.method)
function A() {
var count = 0;
this.aa = "aa";
this.method = function() {
return count;
}
this.obj = {}
}
A.prototype = {
aa:"aa",
method:function(){
}
};
var a = new A;
var b = new A;
console.log(a.aa === b.aa);//true 由于aa的值为基本类型,比较值
console.log(a.obj === b.obj) //false 引用类型,每次进入函数体都要重新创建,因此都不一样。
console.log(a.method === b.method); //false
delete a.method; delete b.method; console.log(a.method === A.prototype.method);//true console.log(a.method === b.method); //true
A.method2 = function(){} //类方法
var c = new A;
console.log(c.method2); //undefined
function A() {};
A.prototype = {
aaa : 1
}
function B() {};
B.prototype = A.prototype;
var b = new B;
console.log(b.aaa); //=> 1;
A.prototype.bb = 2;
console.log(b.bb) //=> 2;
function extend (des, source) { //des = destination
for (var property in source)
des[property] = source[property];
return des;
}
function A() {};
A.prototype = {
aa:function(){
alert(1)
}
}
function bridge() {
};
bridge.prototype = A.prototype;
function B() {}
B.prototype = new bridge();
var a = new A;
var b = new B;
console.log(a == b) //false 证明成功分开原型
console.log(A.prototype == B.prototype) //true 子类共享父类的原型方法
console.log(a.aa === b.aa); //为父类动态添加新的方法
A.prototype.bb = function () {
alert(2)
}
//true,继承父类的方法
B.prototype.cc = function (){
alert(3)
}
//false 父类未必有子类的new实例
console.log(a.cc === b.cc)
//并且它能够正常通过javascript自带的验证机制instanceof
console.log(b instanceof A) ;//true
console.log(b instanceof B) ; //true
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
}
function inherit(init, Parent, proto){
function Son(){
Parent.apply(this, argument); //先继承父类的特权成员
init.apply(this, argument); //在执行自己的构造器
}
}
//由于Object.create是我们伪造的,因此避免使用第二个参数
Son.prototype = Object.create(Parent.prototype,{});
Son.prototype.toString = Parent.prototype.toString; //处理IEbug
Son.prototype.valueOf = Parent.prototype.valueOf; //处理IEbug
Son.prototype.constructor = Son; //确保构造器正常指向,而不是Object
extend(Son, proto) ;//添加子类的特有的原型成员
return Son;
function A(){
}
A.prototype = {
aa:1
}
var a = new A;
console.log(a.aa) ; //=>1
//将它的所有原型都替换掉
A.prototype = {
aa:2
}
console.log(a.aa); //=>1
//于是我们想到每个实例都有一个constructor方法,指向其构造器
//而构造器上面正好有我们的原型,javascript引擎是不是通过该路线回溯属性呢
function B(){
}
B.prototype = {
aa:3
}
a.constructor = B;
console.log(a.aa) //1 表示不受影响
function A(){
console.log(this.__proto__.aa); //1
this.aa = 2
}
A.prototype = {aa:1}
var a = new A;
console.log(a.aa)
a.__proto__ = {
aa:3
}
console.log(a.aa) //=>2
delete a. aa; //删除特权属性,暴露原型链上的同名属性
console.log(a.aa) //=>3
function A() {}
A.prototype = {
aa:1
}
function bridge() {}
bridge.prototype = A.prototype;
function B(){}
B.prototype = new bridge();
B.prototype.constructor = B;
var b = new B;
B.prototype.cc = function(){
alert(3)
}
//String.prototype === new String().__proto__ => true
console.log(B.prototype.__proto__ === A.prototype) //true
console.log(b.__proto__ == B.prototype); //true
console.log(b.__proto__.__proto__ === A.prototype); //true 得到父类的原型对象
var P = (function(prototype, ownProperty, undefined) {
return function P(_superclass /* = Object */, definition) {
// handle the case where no superclass is given
if (definition === undefined) {
definition = _superclass;
_superclass = Object;
}
// C is the class to be returned.
//
// When called, creates and initializes an instance of C, unless
// `this` is already an instance of C, then just initializes `this`;
// either way, returns the instance of C that was initialized.
//
// TODO: the Chrome inspector shows all created objects as `C`
// rather than `Object`. Setting the .name property seems to
// have no effect. Is there a way to override this behavior?
function C() {
var self = this instanceof C ? this : new Bare;
self.init.apply(self, arguments);
return self;
}
// C.Bare is a class with a noop constructor. Its prototype will be
// the same as C, so that instances of C.Bare are instances of C.
// `new MyClass.Bare` then creates new instances of C without
// calling .init().
function Bare() {}
C.Bare = Bare;
// Extend the prototype chain: first use Bare to create an
// uninitialized instance of the superclass, then set up Bare
// to create instances of this class.
var _super = Bare[prototype] = _superclass[prototype];
var proto = Bare[prototype] = C[prototype] = C.p = new Bare;
// pre-declaring the iteration variable for the loop below to save
// a `var` keyword after minification
var key;
// set the constructor property on the prototype, for convenience
proto.constructor = C;
C.extend = function(def) { return P(C, def); }
return (C.open = function(def) {
if (typeof def === 'function') {
// call the defining function with all the arguments you need
// extensions captures the return value.
def = def.call(C, proto, _super, C, _superclass);
}
// ...and extend it
if (typeof def === 'object') {
for (key in def) {
if (ownProperty.call(def, key)) {
proto[key] = def[key];
}
}
}
// if no init, assume we're inheriting from a non-Pjs class, so
// default to using the superclass constructor.
if (!('init' in proto)) proto.init = _superclass;
return C;
})(definition);
}
// as a minifier optimization, we've closured in a few helper functions
// and the string 'prototype' (C[p] is much shorter than C.prototype)
})('prototype', ({}).hasOwnProperty);
var Dog = P (function(proto, superProto){
proto.init = function(name) { //构造函数
this.name = name;
}
proto.move = function(meters){ //原型方法
console.log(this.name + " moved " + meters + " m.")
}
});
var a = new Dog("aaa")
var b = new Dog("bbb"); //无实例变化
a.move(1);
b.move(2);
var Animal = P (function(proto, superProto){
proto.init = function(name) { //构造函数
this.name = name;
}
proto.move = function(meters){ //原型方法
console.log(this.name + " moved " + meters + " m.")
}
});
var a = new Animal("aaa")
var b = new Animal("bbb"); //无实例变化
a.move(1);
b.move(2);
//...............
var Snake = P (Animal, function(snake, animal){
snake.init = function(name, eyes){
animal.init.call(this, arguments); //调运父类构造器
this.eyes = 2;
}
snake.move = function() {
console.log('slithering...');
animal.move.call(this, 5); //调运父类同名方法
}
});
var s = new Snake("snake", 1);
s.move();
console.log(s.name);
console.log(s.eyes);
var Cobra = P (Snake, function(cobra){
var age = 1;//私有属性
//这里还可以编写私有方法
cobra.glow = function(){ //长大
return age++;
}
});
var c = new Cobra("cobra");
console.log(c.glow()); //1
console.log(c.glow()); //2
console.log(c.glow()); //3
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有