- 浏览: 29370 次
- 性别:
- 来自: 北京
最新评论
本文讨论:
|
本文使用了以下技术:
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);
相关推荐
- Python编程基础:熟悉Python语法、数据类型、控制结构、函数和面向对象编程等。 - 网络编程知识:了解HTTP协议、Web请求和响应机制、以及如何使用Python处理网络通信。 - 第三方库使用经验:熟悉并能应用常用的...
它支持多种编程范式,包括面向对象、命令式和函数式编程风格。Python的强大之处在于其丰富的标准库以及广泛的第三方库,这些库大大简化了开发过程,使得开发者能够快速构建复杂的应用程序。 #### 1.2 AI在联系人...
ASP.NET窗体应用程序的特点 **知识点概述**: - ASP.NET窗体程序是在服务器端解释执行的,并非在浏览器中。 - ASP.NET窗体程序可以使用任何.NET兼容的语言进行编写。 - 不同于传统的Web开发方式,ASP.NET窗体程序...
MVC(Model-View-Controller)是一种常用的设计模式,广泛应用于Web应用开发中。该模式将应用程序分为三个主要部分: - **Model(模型)**:负责存储数据和业务逻辑处理。 - **View(视图)**:用户界面,展示数据...
它支持多种编程范式,包括面向对象、命令式、函数式以及过程化编程。Python 的设计哲学强调代码的可读性和简洁的语法,同时也注重提高开发效率。这些特性使得 Python 成为了教育领域和实际项目开发中的首选语言之一...
#### 第五天:面向对象编程 - **类与对象**: - 类的概念及定义。 - 对象的创建与使用。 - 属性与方法。 - **继承与多态**: - 继承的基本概念与实现。 - 多态的含义及其在Python中的体现。 - 封装性与私有...
**SpringMVC**:作为Spring框架的一部分,SpringMVC是一个用于构建Web应用程序的轻量级MVC(Model-View-Controller)框架。它负责处理HTTP请求,将请求映射到处理器,并将结果返回给视图。SpringMVC的核心组件包括...
Python通常用于开发Web应用程序、游戏、数据科学、机器学习以及快速构建各种原型。 ### 源码实例的开发 源码实例指的是编程语言中的具体代码示例,用于演示如何实现特定的功能或操作。在本次案例中,源码实例是指用...
Java Platform, Enterprise Edition(JavaEE)是一个广泛使用的标准,用于开发可伸缩的、健壮的企业级应用程序。它基于Java Standard Edition (Java SE),并通过提供各种服务和API来支持复杂的企业应用。 #### 2. ...
易语言支持面向对象、组件化、跨平台等多种编程技术,并且拥有丰富的内置函数库和开发工具,可以用于开发各种类型的软件产品,包括但不限于桌面应用程序、Web 应用程序、手机应用等。 #### 游戏源码概述 本次介绍...
Java语言的基础包括面向对象编程(OOP)特性、强大的异常处理和垃圾回收机制,以及跨平台的兼容性。 ### Java执行流程 1. 首先,开发者在文本编辑器中创建一个`.java`文件,例如`Test.java`。 2. 在`Test.java`...
7. **面向对象编程**:手册会详细讲解PHP 5.5的面向对象特性,如命名空间(Namespaces)、类和对象、接口(Interfaces)、抽象类(Abstract Classes)以及继承(Inheritance)等。 8. **文件系统操作**:手册会涵盖...
SpringMVC是Spring框架的一个模块,专门用于构建Web应用程序。它实现了MVC(Model-View-Controller)设计模式,其中: - **Model**:业务逻辑层,处理数据操作; - **View**:视图层,显示数据给用户; - **...