javascript OOP整理中

function Animal(name) {
this.name = name;
}

Animal.prototype.run = function() {
console.log(this.name + " is running!!");
}


var a = new Animal('a');
var b = new Animal('b');

console.log(Animal.prototype);
/*===log=== Object
Animal {
run:[function]
}
*/
console.log(Animal.prototype instanceof Object);
/*===log==
true
可知构造函数Animal的prototype属性是一个对象
*/

console.log(Animal.prototype.constructor == Animal);
/*===log==
true
可知构造函数Animal的prototype属性是一个对象,
这个对象有一个constructor属性反过来指向构造函数Animal
*/
console.log(a);
console.log(a.constructor); //[Function: Animal]
console.log(a.__proto__.constructor); //[Function: Animal]
console.log(a.__proto__ == Animal.prototype);
console.log(b.__proto__ == Animal.prototype);
/*===log==
true
可知通过构造函数Animal实例化的a对象具
有一个属性__proto__(神秘的链接)
反过来指向构造函数的原型对象

__proto__在new的时候会自动加载在实例对象上。
*/
console.log(a.__proto__.__proto__);
/*===log==
{}
Object {} 最后会找到最上面的boject对象

原始对象,内置对象
*/
console.log(a.__proto__.run == a.run);
console.log(a.__proto__.run == Animal.prototype.run);

function Dog(name){
//调用父类的构造函数,通过改变this指向将属性赋值到新的实例对象
Animal.call(this,name);
}

//直接将父类实例作为子类的原型,简单粗暴造成多余的原型属性name。还有construct的问题。
Dog.prototype = new Animal();

var dog = new Dog("dog");
dog.run();//dog is running!!

console.log(dog.constructor);
console.log(dog.constructor == Animal);
console.log(dog.constructor == Dog);

 

demo2

function Animal(name) {
this.name = name;
}

Animal.prototype.run = function() {
console.log(this.name + " is running!!");
}

/*修复 造成多余的原型属性name。 及construct的问题。
var F = function(){};
F.prototype = Animal.prototype;
Dog.prototype = new F();
Dog.prototype.constructor = Dog;
*/

function objCreate(prototype){
var F = function(){};
F.prototype = prototype;
return new F();
}

function inherit(subClass,parentClass){
subClass.prototype = objCreate(parentClass.prototype);
subClass.prototype.constructor = subClass;
}

function Dog(name){
//调用父类的构造函数,通过改变this指向将属性赋值到新的实例对象
Animal.call(this,name);
}

inherit(Dog,Animal);
var dog = new Dog('dog');
dog.run(); //dog is runing

/*缺点:
比如需要自己手动维护在构造函数里调用父类构造函数。
同时继承写法对不了接原理的比较容易出错。
*/

# demo3.js 初步oop

(function(){
var initializing = false,
fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
this.Class = function(){};
this.Class.extend = function(prop) {
var _super = this.prototype;
initializing = true;
var prototype = new this();
initializing = false;

for (var name in prop) {
prototype[name] = typeof prop[name] == "function" &&
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
(function(name, fn){
return function() {
var tmp = this._super;
this._super = _super[name];
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, prop[name]) :
prop[name];
}

function Class() {
if ( !initializing && this.init )
this.init.apply(this, arguments);
}

Class.prototype = prototype;
Class.prototype.constructor = Class;
Class.extend = arguments.callee;

return Class;
};
})();

var Person = Class.extend({
init: function(isDancing){
this.dancing = isDancing;
},
dance: function(){
return this.dancing;
}
});

var Ninja = Person.extend({
init: function(){
this._super( false );
},
dance: function(){
// Call the inherited version of dance()
return this._super();
},
swingSword: function(){
return true;
}
});

var p = new Person(true);
p.dance(); // => true

var n = new Ninja();
n.dance(); // => false
n.swingSword(); // => true

// Should all be true
console.log(p instanceof Person && p instanceof Class &&
n instanceof Ninja && n instanceof Person && n instanceof Class);

 

## node服务端 

// import express frame
var express = require('express');
//import mongoose frame
var mongoose = require('mongoose');
//init express
var server = express();

//connect db
var db = mongoose.connect('mongodb://127.0.0.1:27107/product');
db.connection.on('error',function(error){
console.log('数据库链接失败' + error);
});

//Skeleton of data model
var Schema = new mongoose.Schema({
name: {type: String},
description: {type: String},
category: {type: String},
price: {type: Number}
},{
collection: 'product'
});


//get data model
var model = db.model('product',Schema);

server.get('/product',function(request,response){
//search data from db
model.find({},function(error,result){
if(error){
console.log('查询数据失败' + error);
return false;
}
var json = JSON.stringify(result);
var string = request.query.callback + '('+json+')';
response.send(string);
});
});
server.listen(8888);
console.log('The server is already open');

 

posted @ 2017-09-15 15:07  夕阳下的小老头儿  阅读(99)  评论(0编辑  收藏  举报