- 浏览: 201393 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
guji528:
使用Python通过正则表达式替换很方便:
sprin ...
Python正则表达式指南 -
guji528:
很实用,先keep再看
Python正则表达式指南 -
yushine:
1,2,3,5 已经做了剩下的本来也正准备做。
2012, 每一个软件工程师必须做的11件事 -
mynetstudy:
用导出不就可以了吗
递归删除SVN工作目录下的.svn目录
本文讨论:
|
本文使用了以下技术:
JavaScript |
var userObject = new Object(); userObject.lastLoginTime = new Date(); alert(userObject.lastLoginTime);
var userObject = {}; // equivalent to new Object() userObject[“lastLoginTime”] = new Date(); alert(userObject[“lastLoginTime”]);
var userObject = { “lastLoginTime”: new Date() }; alert(userObject.lastLoginTime);
function func(x) { alert(x); } func(“blah”);
var func = function(x) { alert(x); }; func(“blah2”);
var func = new Function(“x”, “alert(x);”); func(“blah3”);
function sayHi(x) { alert(“Hi, “ + x + “!”); } sayHi.text = “Hello World!”; sayHi[“text2”] = “Hello World... again.”; alert(sayHi[“text”]); // displays “Hello World!” alert(sayHi.text2); // displays “Hello World... again.”
// assign an anonymous function to a variable
var greet = function(x) {
alert(“Hello, “ + x);
};
greet(“MSDN readers”);
// passing a function as an argument to another
function square(x) {
return x * x;
}
function operateOn(num, func) {
return func(num);
}
// displays 256
alert(operateOn(16, square));
// functions as return values
function makeIncrementer() {
return function(x) { return x + 1; };
}
var inc = makeIncrementer();
// displays 8
alert(inc(7));
// functions stored as array elements
var arr = [];
arr[0] = function(x) { return x * x; };
arr[1] = arr[0](2);
arr[2] = arr[0](arr[1]);
arr[3] = arr[0](arr[2]);
// displays 256
alert(arr[3]);
// functions as object properties
var obj = { “toString” : function() { return “This is an object.”; } };
// calls obj.toString()
alert(obj);
var myDog = { “name” : “Spot”, “bark” : function() { alert(“Woof!”); }, “displayFullName” : function() { alert(this.name + “ The Alpha Dog”); }, “chaseMrPostman” : function() { // implementation beyond the scope of this article } }; myDog.displayFullName(); myDog.bark(); // Woof!
function displayQuote() {
// the value of “this” will change; depends on
// which object it is called through
alert(this.memorableQuote);
}
var williamShakespeare = {
“memorableQuote”: “It is a wise father that knows his own child.”,
“sayIt” : displayQuote
};
var markTwain = {
“memorableQuote”: “Golf is a good walk spoiled.”,
“sayIt” : displayQuote
};
var oscarWilde = {
“memorableQuote”: “True friends stab you in the front.”
// we can call the function displayQuote
// as a method of oscarWilde without assigning it
// as oscarWilde’s method.
//”sayIt” : displayQuote
};
williamShakespeare.sayIt(); // true, true
markTwain.sayIt(); // he didn’t know where to play golf
// watch this, each function has a method call()
// that allows the function to be called as a
// method of the object passed to call() as an
// argument.
// this line below is equivalent to assigning
// displayQuote to sayIt, and calling oscarWilde.sayIt().
displayQuote.call(oscarWilde); // ouch!
alert(“NaN is NaN: “ + isNaN(NaN)); function x() { this.isNaN = function() { return “not anymore!”; }; } // alert!!! trampling the Global object!!! x(); alert(“NaN is NaN: “ + isNaN(NaN));
Dog spot = new Dog();
function DogConstructor(name) { this.name = name; this.respondTo = function(name) { if(this.name == name) { alert(“Woof”); } }; } var spot = new DogConstructor(“Spot”); spot.respondTo(“Rover”); // nope spot.respondTo(“Spot”); // yeah!
var spot = new DogConstructor(“Spot”);
// create an empty object var spot = {}; // call the function as a method of the empty object DogConstructor.call(spot, “Spot”);
// Think of this as class Dog function Dog(name) { // instance variable this.name = name; // instance method? Hmmm... this.respondTo = function(name) { if(this.name == name) { alert(“Woof”); } }; } var spot = new Dog(“Spot”);
function respondTo() { // respondTo definition } function Dog(name) { this.name = name; // attached this function as a method of the object this.respondTo = respondTo; }
var buddy = new Dog(“Buddy“);
var spot = new Dog(“Spot”);
// Dog.prototype is the prototype of spot
alert(Dog.prototype.isPrototypeOf(spot));
// spot inherits the constructor property
// from Dog.prototype
alert(spot.constructor == Dog.prototype.constructor);
alert(spot.constructor == Dog);
// But constructor property doesn’t belong
// to spot. The line below displays “false”
alert(spot.hasOwnProperty(“constructor”));
// The constructor property belongs to Dog.prototype
// The line below displays “true”
alert(Dog.prototype.hasOwnProperty(“constructor”));
Dog.prototype = new Object();
- 继承原型对象的对象上可以立即呈现对原型所做的更改,即使是在创建这些对象之后。
- 如果在对象中定义了属性/方法 X,则该对象的原型中将隐藏同名的属性/方法。例如,通过在 Dog.prototype 中定义 toString 方法,可以改写 Object.prototype 的 toString 方法。
- 更改只沿一个方向传递,即从原型到它的派生对象,但不能沿相反方向传递。
function GreatDane() { }
var rover = new GreatDane();
var spot = new GreatDane();
GreatDane.prototype.getBreed = function() {
return “Great Dane”;
};
// Works, even though at this point
// rover and spot are already created.
alert(rover.getBreed());
// this hides getBreed() in GreatDane.prototype
spot.getBreed = function() {
return “Little Great Dane”;
};
alert(spot.getBreed());
// but of course, the change to getBreed
// doesn’t propagate back to GreatDane.prototype
// and other objects inheriting from it,
// it only happens in the spot object
alert(rover.getBreed());
function DateTime() { } // set static method now() DateTime.now = function() { return new Date(); }; alert(DateTime.now());
function filter(pred, arr) {
var len = arr.length;
var filtered = []; // shorter version of new Array();
// iterate through every element in the array...
for(var i = 0; i < len; i++) {
var val = arr[i];
// if the element satisfies the predicate let it through
if(pred(val)) {
filtered.push(val);
}
}
return filtered;
}
var someRandomNumbers = [12, 32, 1, 3, 2, 2, 234, 236, 632,7, 8];
var numbersGreaterThan100 = filter(
function(x) { return (x > 100) ? true : false; },
someRandomNumbers);
// displays 234, 236, 632
alert(numbersGreaterThan100);
var greaterThan300 = filter( function(x) { return (x > 300) ? true : false; }, someRandomNumbers);
function makeGreaterThanPredicate(lowerBound) { return function(numberToCheck) { return (numberToCheck > lowerBound) ? true : false; }; }
var greaterThan10 = makeGreaterThanPredicate(10); var greaterThan100 = makeGreaterThanPredicate(100); alert(filter(greaterThan10, someRandomNumbers)); alert(filter(greaterThan100, someRandomNumbers));
function Person(name, age) { this.getName = function() { return name; }; this.setName = function(newName) { name = newName; }; this.getAge = function() { return age; }; this.setAge = function(newAge) { age = newAge; }; }
var ray = new Person(“Ray”, 31); alert(ray.getName()); alert(ray.getAge()); ray.setName(“Younger Ray”); // Instant rejuvenation! ray.setAge(22); alert(ray.getName() + “ is now “ + ray.getAge() + “ years old.”);
function Person(name, age) { var occupation; this.getOccupation = function() { return occupation; }; this.setOccupation = function(newOcc) { occupation = newOcc; }; // accessors for name and age }
Person.prototype.somePublicMethod = function() { // doesn’t work! // alert(this.name); // this one below works alert(this.getName()); };
// class Pet function Pet(name) { this.getName = function() { return name; }; this.setName = function(newName) { name = newName; }; } Pet.prototype.toString = function() { return “This pet’s name is: “ + this.getName(); }; // end of class Pet var parrotty = new Pet(“Parrotty the Parrot”); alert(parrotty);
// class Dog : Pet
// public Dog(string name, string breed)
function Dog(name, breed) {
// think Dog : base(name)
Pet.call(this, name);
this.getBreed = function() { return breed; };
// Breed doesn’t change, obviously! It’s read only.
// this.setBreed = function(newBreed) { name = newName; };
}
// this makes Dog.prototype inherits
// from Pet.prototype
Dog.prototype = new Pet();
// remember that Pet.prototype.constructor
// points to Pet. We want our Dog instances’
// constructor to point to Dog.
Dog.prototype.constructor = Dog;
// Now we override Pet.prototype.toString
Dog.prototype.toString = function() {
return “This dog’s name is: “ + this.getName() +
“, and its breed is: “ + this.getBreed();
};
// end of class Dog
var dog = new Dog(“Buddy”, “Great Dane”);
// test the new toString()
alert(dog);
// Testing instanceof (similar to the is operator)
// (dog is Dog)? yes
alert(dog instanceof Dog);
// (dog is Pet)? yes
alert(dog instanceof Pet);
// (dog is Object)? yes
alert(dog instanceof Object);
var MSDNMagNS = {}; MSDNMagNS.Pet = function(name) { // code here }; MSDNMagNS.Pet.prototype.toString = function() { // code }; var pet = new MSDNMagNS.Pet(“Yammer”);
var MSDNMagNS = {}; // nested namespace “Examples” MSDNMagNS.Examples = {}; MSDNMagNS.Examples.Pet = function(name) { // code }; MSDNMagNS.Examples.Pet.prototype.toString = function() { // code }; var pet = new MSDNMagNS.Examples.Pet(“Yammer”);
// MSDNMagNS.Examples and Pet definition... // think “using Eg = MSDNMagNS.Examples;” var Eg = MSDNMagNS.Examples; var pet = new Eg.Pet(“Yammer”); alert(pet);
function object(o) { function F() {} F.prototype = o; return new F(); }
MyNamespace.MyClass = function() { MyNamespace.MyClass.initializeBase(this); this._myProperty = null; } Then, you need to define the class members itself in its prototype: MyNamespace.MyClass.prototype = { get_myProperty: function() { return this._myProperty;}, set_myProperty: function(value) { this._myProperty = value; }, doSomething: function() { MyNamespace.MyClass.callBaseMethod(this, “doSomething”); /* do something more */ } }
MyNamespace.MyClass.registerClass( “MyNamespace.MyClass “, MyNamespace.BaseClass);
发表评论
-
Highchart Vs Flot.js – Comparing JavaScript Graphing Engines
2012-09-06 02:18 1763In previous projects at MailCha ... -
JavaScript世界的一等公民 - 函数
2012-08-15 13:54 828简介 在很多传统语言( ... -
jQuery:click() bind() live() delegate()区别
2012-08-03 10:30 1080click(),bind(),live()执行事件方法区别: ... -
三谈iFrame框架自适应高度
2012-07-22 00:03 1053为什么是三谈 为什么是三谈呢?一是因为这真的是一个被说烂 ... -
js中date对象扩展的相关函数
2011-10-14 15:03 1687该文章收集了对js中date对象扩展的相关函数,包括:把字符串 ... -
领悟 JavaScript 中的面向对象
2011-09-29 15:57 390领悟 JavaScript 中的面向 ... -
Firebug控制台详解
2011-08-11 15:24 770作者: 阮一峰 日期: 2011年3月26日 ... -
Backbone JS框架指南
2011-05-27 22:49 2894Posted Mar.13, 2011 unde ... -
10个最佳jQuery Lightbox效果插件收集
2011-04-10 01:25 1223原文:10个最佳jQuery Lightbox效果插件收 ... -
JS获取当前页面的URL信息
2011-02-23 10:14 1604js和php一样,都有获取url的函数,一直没有用过,今天需 ... -
输入法下keyup失效的解决方案
2011-01-17 10:02 2958【转】http://realazy.org/blog/2007 ... -
JOHN RESIG的膜拜帖
2010-10-12 15:56 1239JOHN RESIG, jquery的创造者, 被认为是js世 ...
相关推荐
本文将讨论使用面向对象技术创建高级Web应用程序的方法。面向对象编程(OOP)是一种流行的编程方法,许多JavaScript库中都使用这种方法,以便更好地管理和维护基本代码。JavaScript支持OOP,但与诸如C++、C#或Visual...
JavaScript,作为一种广泛应用于Web开发的脚本语言,以其动态、灵活和强大的功能,成为构建现代高级Web应用程序的基础。本文将深入探讨如何利用JavaScript的面向对象技术来构建这些复杂的应用程序。 面向对象编程...
### JavaScript使用面向对象的技术创建高级Web应用程序 #### JavaScript对象的本质:词典模型 在深入了解如何使用面向对象的技术创建高级Web应用程序之前,理解JavaScript中的对象是如何工作的至关重要。与C++或C#...
JavaScript是一种广泛用于Web开发的脚本语言,尤其在创建高级Web应用程序时,其面向对象的特性显得尤为重要。本文档将深入探讨如何利用JavaScript的面向对象技术来构建复杂的Web应用。 面向对象编程(Object-...
JavaScript 面向对象技术是构建复杂Web应用程序的核心部分。在C++或C#中,对象基于类或结构实例化,拥有特定...这些特性使得JavaScript成为构建高级Web应用程序的强大工具,能够适应各种复杂的业务逻辑和用户交互需求。
### JavaScript面向对象技术创建高级Web应用程序 #### 一、JavaScript对象模型 JavaScript作为一种灵活且功能强大的脚本语言,在Web开发领域扮演着极其重要的角色。面向对象编程(OOP)是现代软件工程的核心概念之一...
面向对象技术在创建高级Web应用程序中的应用主要涉及JavaScript的面向对象特性,因为JavaScript是Web开发中广泛使用的脚本语言。JavaScript中的面向对象编程(OOP)与其他如C++、C#或Visual Basic等.NET框架兼容语言...
《C# Web应用程序入门经典_程序设计》是一本专为初学者设计的IT技术书籍,主要涵盖了使用C#语言开发Web应用程序的基础知识和实践技巧。这本书对于那些希望通过学习C#来构建动态、交互式Web应用的读者来说,是理想的...
【面向.NET的Web应用程序设计】课程的课后答案涵盖了.NET Framework的基本概念及其核心组成部分,以及Microsoft Visual Studio .NET的使用技巧。以下是对这些知识点的详细解释: 1. **Microsoft .NET Framework**:...
Web应用程序使用的技术一般包括HTML、CSS和JavaScript,而服务器端则可能使用多种服务器端技术,比如***。***是.NET框架的一部分,它允许开发者用C#语言来创建动态网站、Web应用程序和Web服务。 《C# + Web应用程序...
C# Web应用程序是基于.NET...总结来说,C# Web应用程序开发涵盖了从基础的C#语法到ASP.NET框架的高级特性,再到数据库交互、前端技术和部署策略等多个层面。通过学习这些知识点,你将具备开发现代Web应用程序的能力。
- **Web建模**:探讨如何使用UML对Web应用进行建模。 - **UML与设计模式**:结合设计模式进一步加深对UML的理解。 - **OO程序设计语言**:教授如何使用Java或其他面向对象编程语言实现面向对象设计。 - **RUP ...
在Web开发方面,Visual Studio 2005支持ASP.NET技术,这是一种基于服务器的Web应用程序框架,用于构建动态网站、Web应用程序和Web服务。ASP.NET提供了多种开发模型,如Web Forms、ASP.NET MVC和ASP.NET Web Pages,...
总的来说,这两个面向对象程序设计的课程方向提供了全面的技能训练,覆盖了桌面应用和Web应用开发的关键领域。通过学习,学生不仅能深入理解面向对象编程的核心理念,还能掌握实际开发中必不可少的技术,从而更好地...
第1章: 概述 第 2 章:C#与Visual Studio 2005 第 3 章:变量、数据类型和表达式 第 4 章:分支和循环 第 5 章:面向对象 第 6 章:面向对象的高级...第 10 章:创建 Web 应用程序 第 11 章:.NET Framework 2.0 简介