javascriptдÀ෽ʽ֮Èý
È¡Ç°ÃæÁ½ÖÖµÄÓŵ㣺
a¡¢Óù¹Ô캯ÊýÀ´¶¨ÒåÀàÊôÐÔ£¨×ֶΣ©
b¡¢ÓÃÔÐÍ·½Ê½À´¶¨ÒåÀàµÄ·½·¨¡£
¾ÍÓÐÁ˵ÚÈýÖÖ·½Ê½¡£ÕâÖÖ·½Ê½Ã²ËƲÉÓõÄÈ˽϶ࡣ
3¡¢×ۺϹ¹Ô캯Êý/ÔÐÍ
/**
* PersonÀࣺ¶¨ÒåÒ»¸öÈË£¬ÓиöÊôÐÔname£¬ºÍÒ»¸ögetName·½·¨
* @param {String} name
*/
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
ÕâÑù£¬¼´¿Éͨ¹ý¹¹Ô캯Êý¹¹Ô첻ͬnameµÄÈË£¬¶ÔÏóʵÀýÒ²¶¼¹²ÏígetName·½·¨£¬²»»áÔì³ÉÄÚ´æÀË·Ñ¡£
µ«ËƺõÕâÑùµÄ´úÂë·ç¸ñËÆºõÈÔȻûÓÐjavaµÄÀàÄÇô½ô´Õ£¬°ÑÊôÐÔ£¬¹¹Ôì·½·¨£¨º¯Êý£©£¬·½·¨¶¼°üÔÚ´óÀ¨ºÅÄÚ¡£
public class Person {
//ÊôÐÔ£¨×ֶΣ©
String name;
//¹¹Ôì·½·¨£¨º¯Êý£©
Person(String name) {
this.name = name;
}
//·½·¨
String getName() {
return this.name;
}
}
ΪÁËÈÃjs´úÂë·ç¸ñ¸ü½ô´Õ£¬°Ñ¹ÒÔÚprototypeµÄ·½·¨´úÂëÒÆµ½function PersonµÄ´óÀ¨ºÅÄÚ¡£
function Person(name) {
this.name = name;
Person.prototype.getName = function() {
return this.name;
}
}
ËÆºõºÜÉñÆæ£¬»¹ÄÜÕâôд°¡£¡ÑéÖ¤Ò»ÏÂ
var p1 = new Person("Jack");
var p2 = new Person("Tom");
console.log(p1.getName());//Jack
console.log(p2.getName());//Tom
ûÓб¨´í£¬¿ØÖÆÌ¨Ò²ÕýÈ·Êä³öÁË¡£ËµÃ÷¿ÉÒÔÕâôд£¬ºÇºÇ¡£
àÅ£¬ËƺõºÜÍêÃÀ¡£
a ¡¢¿ÉÒÔͨ¹ý´«²Î¹¹Ôì¶ÔÏóʵÀý
b ¡¢¶ÔÏóʵÀý¶¼¹²Ïíͬһ·Ý·½·¨²»Ôì³ÉÄÚ´æÀË·Ñ
c ¡¢´úÂë·ç¸ñÒ²±È½Ï½ô´Õ
µ«Ã¿´ÎnewÒ»¸ö¶ÔÏóµÄʱºò¶¼»áÖ´ÐÐ
Person.prototype.getName = function() {
return this.name;
}
Ôì³ÉÁ˲»±ØÒªµÄÖØ¸´µÄÔËËã¡£ÒòΪgetName·½·¨¹ÒÔÚprototypeÉÏÖ»ÐèÖ´ÐÐÒ»´Î¼´¿É¡£Ö»ÐèÉÔ΢¸ÄÔìÏ£º
function Person(name) {
this.name = name;
if(Person._init==undefined) {
alert("ÎÒÖ»Ö´ÐÐÒ»´Î£¡");
Person.prototype.getName = function() {
return this.name;
}
Person._init = 1;
}
}
newÁ½¸ö¶ÔÏó£¬
var p1 = new Person("Andy");//µÚÒ»´Înew»áµ¯³ö'ÎÒÖ»Ö´ÐÐÒ»´Î£¡'
var p2 = new Person("Lily");//ÒÔºónewµÄ¶ÔÏó²»»áÔÙÖ´ÐÐÁË
Ïà¹ØÎĵµ£º
Õâ¸öËæ±ÊÆäʵÊÇΪÁ˸ÐлÇå·çЦ¸øµÄÒ»¸öÌáʾ£¬²»½ö½öÊǸæËßÎÒÔõôÅжÏÊý×飬¸üÈÃÎÒÓÐÁËÈÏÕæ¶ÁÒ»¶Á ¡¶javascriptȨÍþÖ¸ÄÏ¡·µÄÏë·¨¡£
±È½ÏºÍ¿½±´ÆäʵÊÇÒ»»ØÊ£¬´úÂëÈçÏ£º
//
//Compare object function
//
function Compare(fobj,sobj)
{
var ftype = typ ......
calcu() ----º¯ÊýʵÏÖÁ½¸öÊýÏà³Ë
document.write(); ---´òÓ¡
document.calc.ÔªËØÃû.value; --»ñµÃ±íµ¥µÄÖµ
document.±íµ¥Ãû¡£±íµ¥ÔªËØÃû¡£value Ϊ±íµ¥¸³Öµ
¶¨Ò庯Êý£º
function Ãû×Ö()
{
//javaScriptÔ¤Óï¾ä
}
onClick="º¯ÊýÃû";  ......
ȺÀïÌÖÂÛ¹ýµÄÌâÄ¿£¬·ÖÏíһϡ£
function test(){
var m=n=1;
alert(m);
}
alert(n);
//ÒòΪtestº¯ÊýûÓÐÖ´ÐУ¬³Ì¶È¿ØÖÆÁ÷²»ÄܽøÈë½øÐнâÎö£¬ÀïÃæµÄ¶«Î÷¶ÔÍâÃæ²»¿É¼û£¬Òò´Ë±¨´í
function test(){
var m=n=1;
alert(m);
}
test();
alert(n);
//µ±testÖ´Ðкó£¬Í ......
2¡¢ÔÐÍ·½Ê½
/**
* PersonÀࣺ¶¨ÒåÒ»¸öÈË£¬ÓиöÊôÐÔname£¬ºÍÒ»¸ögetName·½·¨
*/
function Person(){}
Person.prototype.name = "jack";
Person.prototype.getName = function() { return this.name;}
°ÑÀàµÄÊôÐÔ£¨×ֶΣ©£¬·½·¨¶¼¹ÒÔÚprototypeÉÏ¡£
Ô켸¸ö¶ÔÏó²âÊÔÏ£º
var p1 = new Person();
var ......