- 浏览: 401261 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
You can subclass native object, the most often sub-classed object is the Object class itself.
Let's first see some cases that you do with sub-classing the object class.
Object.prototype.keys1 = function () { var keys1 = []; for (var i in this) { keys1.push(i); } return keys1; };
However, the gotchas above is that when you call keys1(), it may returns more properties than youmight expect it to returns. let's see the example below.
var obj = { a: 1, b: 2, c: 3 }; assert(obj.keys1().length != 3, "The 3 existing properties plus the new keys method keys1, and others");
so you might take care of which properties are on the actual object rather than on inherited prototyped object.
Below is the improved version.
Object.prototype.keys = function () { var keys = []; for (var i in this) { if (this.hasOwnProperty(i)) { keys.push(i); } } return keys; };
And below is the test code
var obj = { a: 1, b: 2, c: 3 }; assert(obj.keys().length == 3, "Only the 3 exiting properties are included.");
Extending the Array.
due to some reason, the IE does not support directly sub-classing the Array, because the property of length of Array is immutable. but you can take the following workaround.
/** * @MyArray now fake the interface of Array, but it does not inherit from Array directly now. */ function MyArray() { } // this length property is to simulate the Array length property, of which is essential to simulate native array object MyArray.prototype.length = 0; (function () { var methods = ['push', 'pop', 'shift', 'unshift', 'slice', 'splice', 'join']; for (var i = 0; i < methods.length; i++) { (function(name) { MyArray.prototype[name] = function() { return Array.prototype[name].apply(this, arguments); }; })(methods[i]); } })();
and then you can do the test via the following code.
var mine = new MyArray(); mine.push(1, 2, 3); assert(mine.length == 3, "All the items are on our sub-classed array."); assert(!(mine instanceof Array), "We are sub-classing array, though");
Extending numbers
You might think you can override the number class so that you can add extra functions to the function literal; However, this is not always true, because of the way how the Javascript engine parse numbers and its properties of the number, it might not work as you expected, as illustrated in the following example.
Number.prototype.add = new function (num) { return this + num; }
and the following code shows the tests and its results.
var n = 5; assert(n.add(3) == 8, "It works fine if the number is an varaible."); alert((5).add(3) == 8, " Also works if a number is wrapping in parenthesis.");
Following is the full code.
/************************************** *@Summary * This shows how you can subclass some native object from javascript * * this utilize this feature to extend the element in the HTML * * @Usage: * * @TODO: * test it ***************************************/ /** * First attempt to subclass the native Array object * * @Comment: there are some restriction by the IE explorer in that it simply does not allow the Array type to be inherted, the reason is becaue the length property is immutable */ //function MyArray() { } //MyArray.prototype = new Array(); //var mine = new MyArray(); //mine.push(1, 2, 3); //assert(mine.length == 3, "All the items are on our sub-classed array."); //assert(mine instanceof Array, "Verify that we implement Array functionality."); Object.prototype.keys1 = function () { var keys1 = []; for (var i in this) { keys1.push(i); } return keys1; }; Object.prototype.keys = function () { var keys = []; for (var i in this) { if (this.hasOwnProperty(i)) { keys.push(i); } } return keys; }; /** * @MyArray now fake the interface of Array, but it does not inherit from Array directly now. */ function MyArray() { } // this length property is to simulate the Array length property, of which is essential to simulate native array object MyArray.prototype.length = 0; (function () { var methods = ['push', 'pop', 'shift', 'unshift', 'slice', 'splice', 'join']; for (var i = 0; i < methods.length; i++) { (function(name) { MyArray.prototype[name] = function() { return Array.prototype[name].apply(this, arguments); }; })(methods[i]); } })(); Number.prototype.add = new function (num) { return this + num; }
and its harnessed test.
<!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> <title></title> <script type="text/javascript" src="../unit.js"></script> <script type="text/javascript" src="subclassnative.js"></script> <script type="text/javascript"> var mine = new MyArray(); mine.push(1, 2, 3); window.onload = function () { test("subclass native methods test", function () { var mine = new MyArray(); mine.push(1, 2, 3); assert(mine.length == 3, "All the items are on our sub-classed array."); assert(!(mine instanceof Array), "We are sub-classing array, though"); }); test("subclass native method test2", function () { var obj = { a: 1, b: 2, c: 3 }; assert(obj.keys1().length == 5, "The 3 existing properties plus the new keys method keys1, and keys"); assert(obj.keys().length == 3, "Only the 3 exiting properties are included."); // does not work, cause syntax error // assert(5.add(3) == 8, "Doesnot work, cause syntax error."); var n = 5; assert(n.add(3) == 8, "It works fine if the number is an varaible."); alert((5).add(3) == 8, " Also works if a number is wrapping in parenthesis."); } ); } </script> <style type="text/css" > #results li.pass { color: Green } #results li.fail { color: Red } </style> </head> <body> <ul id="results" /> </body> </html>
发表评论
-
javascript - trick to cross browser DOM ready event
2012-08-24 08:23 930the "ready" event ... -
javascript - trick to simulate mouseenter and mouseleave
2012-08-23 08:31 2256Previously we discussed javasc ... -
javascript - trick to simulate the change event
2012-08-22 08:51 1671In the previous discussion a ... -
javascript - trick to simulate bubbling submit event
2012-08-22 08:03 909In the previous discussion abou ... -
javascript - trick to implement bubbling submit event
2012-08-23 07:55 707Following up to the javascrip ... -
javascript - trick to detect bubbling supportability
2012-08-20 22:22 975Event delegation is oe of the b ... -
javascript - trigger event and custom events
2012-08-20 21:58 2084In the previous post - javascri ... -
javascript - trick to handlers management
2012-08-20 08:19 1033We have discussed "javascr ... -
javascript - trick to centralized store
2012-08-20 07:52 823For a number of reasons it's ... -
javascript - trick to fix the event object
2012-08-20 07:47 886Many browsers, especially In ... -
javascript - tricks to deal with colors
2012-08-15 08:34 770There are a couple of ways to r ... -
javascript - trick to manipulate the opacity
2012-08-15 08:26 770All other browsre may have supp ... -
javascript - trick to test visibility of an element
2012-08-15 08:15 525though there is a visible prope ... -
javascript - trick to get and set height and width
2012-08-15 08:05 550when looking at properties t ... -
javascript - trick to set/get attributes that expects px values
2012-08-16 11:00 521When setting a number into a ... -
javascript - trick to get and set CSS style
2012-08-16 11:00 749while it will not be so much tr ... -
javascript - trick to normalize href for IE
2012-08-16 10:59 538IE is again the only browser th ... -
javascript - trick IE form and its expando attribute
2012-08-16 10:59 1044there is a known issue that if ... -
javascript expando and attributes
2012-08-14 08:15 1045expando is something like this ... -
javascript - trick to getText and setText
2012-08-14 07:40 1150it is not as simple as you thin ...
相关推荐
Objects and Inheritance Chapter 18. Arrays Chapter 19. Regular Expressions Chapter 20. Dates Chapter 21. Math Chapter 22. JSON Chapter 23. Standard Global Variables Chapter 24. Unicode and JavaScript...
Softgroup .NET Subclass and Hook Objects supply .Net fast, small and lightweight wrapper classes that made easy and secure subclassing and hooking windows for any programmers. Included Classes ...
This demonstrates hoe to write a subclassing control. And it has many examples as to how to use the control (included.)
标题中的"A freeware MFC class to support MFC subclassing"是指一个免费的MFC(Microsoft Foundation Class)类库,它的主要功能是支持MFC的子类化机制。MFC是微软提供的一套C++类库,它封装了Windows API,方便...
The json module: JavaScript Object Notation The plistlib module: A Property-List Parser ctypes Enhancements Improved SSL Support Deprecations and Removals Build and C API Changes Port-Specific ...
在Windows应用程序开发中,MFC(Microsoft Foundation Classes)是一个强大的库,它提供了对Windows API的封装,使得开发者能够更容易地创建用户界面。本主题聚焦于使用MFC来子类化列表视图控件,这是一种常见的任务...
Doing Overlays Using Framebuffer Objects Chapter 21. Creating Plugins Extending Qt with Plugins Making Applications Plugin-Aware Writing Application Plugins Chapter 22. Application Scripting...
Subclassing MFC Objects About Object-Oriented Design Can You See My House from Here? Summary Chapter 8: Using the Windows Common Controls Overview Common Control Basics Initializing the ...
【标题】"subclass-dance-party" 是一个基于JavaScript技术的学生合作项目,旨在探索和实践面向对象编程中的子类化(Subclassing)概念。在这个项目中,开发者可能使用了JavaScript的ES6语法来创建类(Class)并实现...
在编程中,子类化(Subclassing)是一种面向对象编程的概念,它允许我们创建一个新的类(子类)继承自另一个类(父类),并可以扩展或修改父类的行为。在这里,我们将创建一个子类,继承自HTML的`<textarea>`元素或...
Swift是Apple公司推出的一种编程语言,专为iOS、macOS、watchOS和tvOS等平台设计,具有简洁、安全和高性能的特点。在这个“swift demo 包含登陆登出 四个底部TabBar按钮”的项目中,我们可以看到一个典型的iOS应用...