`
modun
  • 浏览: 149932 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

javascript Classical Inheritance

阅读更多
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>javascript 面向对象继承工具</title>
</head>
<body>
<script language="javascript">
	// A simple helper that allows you to bind new functions to the 
// prototype of an object 
Function.prototype.method = function(name, func) { 
     this.prototype[name] = func; 
     return this; 
}; 

// A (rather complex) function that allows you to gracefully inherit 
// functions from other objects and be able to still call the           'parent' 
// object's function 
Function.method('inherits', function(parent) { 
     // Keep track of how many parent-levels deep we are 
     var depth = 0; 

     // Inherit the parent's methods 
     var proto = this.prototype = new parent(); 

     // Create a new 'priveledged' function called 'uber', that when called 
     // executes any function that has been written over in the inheritance 
     this.method('uber', function uber(name) { 

         var func; // The function to be execute 
         var ret; // The return value of the function 
         var v = parent.prototype; // The parent's prototype 

         // If we're already within another 'uber' function 
         if (depth) { 
              // Go the necessary depth to find the orignal prototype 
              for ( var i = d; i > 0; i += 1 ) { 
                  v = v.constructor.prototype; 
              } 

              // and get the function from that prototype 
              func = v[name]; 
			        // Otherwise, this is the first 'uber' call 
                  } else { 
                       // Get the function to execute from the prototype 
                       func = proto[name]; 

                       // If the function was a part of this prototype 
                       if ( func == this[name] ) { 
                            // Go to the parent's prototype instead 
                            func = v[name]; 
                       } 
                  } 

                  // Keep track of how 'deep' we are in the inheritance stack 
                  depth += 1; 

                  // Call the function to execute with all the arguments but the first 
                  // (which holds the name of the function that we're executing) 
                  ret = func.apply(this, Array.prototype.slice.apply(arguments, [1])); 

                  // Reset the stack depth 
                  depth -= 1; 

                  // Return the return value of the execute function 
                  return ret; 
              }); 

              return this; 
}); 

 // A function for inheriting only a couple functions from a parent object, 
 // not every function using new parent() 
 Function.method('swiss', function(parent) { 
	  // Go through all of the methods to inherit 
	  for (var i = 1; i < arguments.length; i += 1) { 
		  // The name of the method to import 
		  var name = arguments[i]; 

		  // Import the method into this object's prototype 
		  this.prototype[name] = parent.prototype[name]; 
	  } 

	  return this; 
 }); 
// Create a new Person object constructor 
function Person( name ) { 
     this.name = name; 
} 

// Add a new method to the Person object 
Person.method( 'getName', function(){ 
     return name; 
}); 

// Create a new User object constructor 
function User( name, password ) { 
     this.name = name; 
     this.password = password; 
}

// Inherit all the methods from the Person object 
User.inherits( Person ); 

// Add a new method to the User object 
User.method( 'getPassword', function(){ 
     return this.password; 
}); 
// Overwrite the method created by the Person object, 
// but call it again using the uber function 
User.method( 'getName', function(){ 
	return "My name is: " + this.uber('getName'); 
});
</script>
</body>
</html>

 虽然自己初学 JavaScript  对这个util 工具脚本还不是很理解  同时也发现JavaScript真的很强大,很灵活,相对于java它真的太灵活了,但我相信随着我学习的深入及实践的增多,我会慢慢的理解它的!

分享到:
评论

相关推荐

    JavaScript Object Programming(Apress,2015)

    This brief book explains the advantages of the object model, inheritance, both classical and prototypical, and shows how these concepts can be implemented in JavaScript. It also shows how object ...

    JavaScript 继承详解(四)

    Classical Inheritance in JavaScript。 Crockford是JavaScript开发社区最知名的权威,是JSON、JSLint、JSMin和ADSafe之父,是《[removed] The Good Parts》的作者。 现在是Yahoo的资深JavaScript架构师,参与YUI的...

    Sammie Bae - JavaScript Data Structures and Algorithms - 2019.pdf

    classical inheritance pattern allows inheritance by creating a blueprintlike form that objects follow during inheritance. However, the prototypal inheritance pattern means copying the objects and ...

    JavaScript Data Structures and Algorithms.epub

    The classical inheritance pattern allows inheritance by creating a blueprint-like form that objects follow during inheritance. However, the prototypal inheritance pattern means copying the objects ...

    讲解JavaScript的面向对象的编程

    3. 使用IE运行Inheritance for Javascript.html文档之后,查看源代码,该代码说明了怎样在JavaScript实现类的继承:使用prototype inheritance(原型继承方式)和classical inheritance(古典继承方式)。...

    Learning JavaScript Design Patterns 英文原版.js设计模式

    If you want to write beautiful, structured, and maintainable JavaScript code, this guide shows you how to apply both classical and modern design patterns to the language. The patterns in this book ...

    js-test-basic:JavaScript Test 基础训练

    ##Augment 简介The world's smallest and fastest classical JavaScript inheritance pattern, augment, is a seven line function which allows you to write CoffeeScript style classes with a flair of ...

    前端开发必须知道的JS之原型和继承

    ### 三、组合继承(Classical Inheritance) 在JavaScript中,通常结合构造函数继承和原型继承来实现更完善的继承机制,如上例所示。这样,不仅可以继承构造函数的属性,还可以继承原型的属性和方法。 ### 四、...

Global site tag (gtag.js) - Google Analytics