要学好JavaScript,有几个基本概念必须搞清楚:prototype,executing,context,closure。Prototype 在JavaScript语言中,通常使用Prototype来实现OO。在这里,我们不对JavaScript的OO实现进行过多的探讨,着重来看一下JS中对象的内存模型。在开始之前,需要先明确以下几点: 1. JS中,存在以下几种数据类型:string,number,boolean,object,function(注意:首字母均为小写)。 2 “Object”, “String”, “Date”等内置数据类...
JavaScript中的prototype概念恰如其分地反映了这个词的内含,我们不能将其理解为C++的prototype那种预先声明的概念。 JavaScript 的所有function类型的对象都有一个prototype属性。这个prototype属性本身又是一个object类型的对象,因此我们也可以给这个prototype对象添加任意的属性和方法。既然prototype是对象的“原型”,那么由该函数构造出来的对象应该都会具有这个“原型”的特性。事实上,在构造函数的prototype上定义的所有...
一、 起因 那天用到prototype.js于是打开看看,才看几行就满头雾水,原因是对js的面向对象不是很熟悉,于是百度+google了一把,最后终于算小有收获,写此纪念一下^_^。 prototype.js代码片段 代码如下:var Class = { create: function() { return function() { this.initialize.apply(this , arguments); } } } // Class使用方法如下 var A = Class.create(); A. prototype={ initialize:function(v){ this .value=v; } showValue...
代码如下:// code from jb51.net function Person(name) { this.Name = name; } Person.prototype.SayHello = function() { alert(Hello, + this.Name); } Person.prototype.SayBye = function() { alert(Goodbye, + this.Name); } 不过,有的时候,为了书写以及维护的方便,我们会把公有方法的声明写到一个对象里,然后赋值给 Person.prototype,例如: 代码如下:// code from jb51.net function Person(name) { this.Name = n...
$w方法 Splits a string into an Array, treating all whitespace as delimiters. Equivalent to Ruby's %w{foo bar} or Perl's qw(foo bar). 代码如下:function $w(string) { if (!Object.isString(string)) return []; string = string.strip(); return string ? string.split(/\s+/) : []; } 这个方法就是用空白字符把字符串分成数组,然后返回。 例子: 代码如下: $w(apples bananas kiwis) // -> [apples, bananas, kiwis] ...
$A方法: Accepts an array-like collection (anything with numeric indices) and returns its equivalent as an actual Array object. This method is a convenience alias of Array.from, but is the preferred way of casting to an Array. 代码如下:function $A(iterable) { if (!iterable) return []; if (toArray in Object(iterable)) return iterable.toArray(); var length = iterable.length || 0, results = new Array...
看一下源码: 代码如下: Date.prototype.toJSON = function() { return " + this.getUTCFullYear() + - + (this.getUTCMonth() + 1).toPaddedString(2) + - + this.getUTCDate().toPaddedString(2) + T + this.getUTCHours().toPaddedString(2) + : + this.getUTCMinutes().toPaddedString(2) + : + this.getUTCSeconds().toPaddedString(2) + Z"; }; 其实就是返回Date的JSON字符串,下面给出示例: 代码如下: new Date(1969, 1...
$ $$ $A $F $H $R $w Try.these document.getElementsByClassName $方法——被成为瑞士军刀(Swiss Army knife) If provided with a string, returns the element in the document with matching ID; otherwise returns the passed element. Takes in an arbitrary number of arguments. All elements returned by the function are extended with Prototype DOM extensions. 代码如下:function $(element) { if (arguments.leng...
这个对象就是对function的一些扩充,最重要的当属bind方法,prototype的帮助文档上特意说了一句话:Prototype takes issue with only one aspect of functions: binding.其中wrap方法也很重要,在类继承机制里面就是利用wrap方法来调用父类的同名方法。argumentNames bind bindAsEventListener curry defer delay methodize wrap 代码如下://通过Object对象的extend方法对Function的prototype进行扩展 Object.extend(Function.pro...
环境: Prototype Version: '1.6.1_rc3' Aptana Studio, build: 1.2.5.023247 IE7 FF2.0.0.4 Opera 10 beta 代码如下:var Prototype = { Version: 1.6.1_rc3, //定义浏览器对象 Browser: (function(){ var ua = navigator.userAgent; var isOpera = Object.prototype.toString.call(window.opera) == [object Opera]; return { IE: !!window.attachEvent && !isOpera, Opera: isOpera, WebKit: ua.indexOf(AppleWebKit/) > -1, Ge...
Enumerable是Prototype框架的基石,而Enumerable不单独使用,在Prototype中其它对象mix了Enumerable里面的方法,这样就可以在这些对象上应用Enumerable的方法,这样的对象有:Array,Hash,ObjectRange,还有一些和DOM,AJAX相关的对象。Enumerable provides a large set of useful methods for enumerations, that is, objects that act as collections of values. It is a cornerstone of Prototype. Enumerable is what we like...
Object is used by Prototype as a namespace; that is, it just keeps a few new methods together, which are intended for namespaced access (i.e. starting with “Object.”). 上面说的namespace个人理解就相当于C#中的静态类,提供工具函数的意思,和C#中的namespace应该不是一个概念。因为C#中的命名空间后面不会直接跟方法,肯定是接一个对象然后在调用方法,不过和C++中的命名空间倒是有些类似 clone extend inspect isA...
代码如下:Object.extend(Number.prototype, (function() { //返回十六进制颜色之 function toColorPart() { return this.toPaddedString(2, 16); } //返回连续的下一个数值 function succ() { return this + 1; } //连续执行某个操作 function times(iterator, context) { $R(0, this, true).each(iterator, context); return this; } //返回固定长度的字符串,前面补0 function toPaddedString(length, radix) { var string =...
Ranges represent an interval of values. The value type just needs to be “compatible,” that is, to implement a succ method letting us step from one value to the next (its successor). Prototype provides such a method for Number and String, but you are of course welcome to implement useful semantics in your own objects, in order to enable ranges based on them. ObjectRange对象基本就是实现了连续的数字...
代码如下:RegExp.prototype.match = RegExp.prototype.test; RegExp.escape = function(str) { return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, \\$1); };就一个escape方法,就是把那几个特殊字符转义一下。 还有就是match方法是test方法的别名。 看一个例子: var str=RegExp.escape("+.[]$://!"); document.writeln(str) //==> "\+\.\[\]\$\:\/\/\!"