`
xly_971223
  • 浏览: 1283827 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

quick guide to somewhat advanced javascript

阅读更多
js火了好长时间了,一直也没去学习一下,无意中发现一片快速指南 照着做了一下 发现原来很简单,现记录下来 作为备忘吧
JSON
json相当于java中的对象:

var user = {
    username:'xuly',
    sex:'男',
    sayHello:function(){
        alert('hello world');
    }
};
user.sayHello();

输出:hello world
在js中函数也是一个对象,你可以传递一个函数作为参数,就像传递一个字符串一样

数组
这两句是一样的
var a = new Array();  //alert(a.size) == 0
var b = [];           //alert(b.size) == 0;

b = ['hello', 34, 'java script'];
b[10] = 32; //alert(b.length) == 11

数组元素可为任何类型 数组型 字符串都是合法的

另外数组下标可以为字符串,主要用于访问对象的属性 如下例
var user = {};
user['username'] = 'xuly';
user['sex'] = '男';
user['sayHello']=function(){
    alert('hello world');
};

这效果跟上面json的user对象是一样的
user['sayHello'](); 
user.sayHello();

这两句也是做同一件事的



js中类的概念是最重要的  下面我们来认识一下类
//定义一个类 :Person
var Person = function(name, age){
    this.name = name;
    this.age = age;
};

var aPerson = new Person('xuly', 20);
alert(aPerson.name); //  output 'xuly'

可以看到js的类就是一个函数, 不同的是这个函数用到了this关键字来定义类属性
上例中只是声明了属性 并没有声明方法 方法是这样声明的
Person.prototype.sayHello = function(){
    return 'hello ' + this.name;
};


如果采用prototype.js框架来定义一个类将更加简单
var Persion = Class.create();
Persion.prototype = {
   //constructor
    initialize:function(name, age){
        this.name = name;
        this.age = age;
    },
    sayHello: function(){
        return 'hello ' + this.name;
    }
};



原文:http://www.sergiopereira.com/articles/advjs.html
分享到:
评论

相关推荐

    javascirpt权威指南中英版

    The name “JavaScript” is actually somewhat misleading. Except for a superficial syn- tactic resemblance, JavaScript is completely different from the Java programming lan- guage. And JavaScript has ...

    Mastering+JavaScript+Functional+Programming-Packt+Publishing(2017).pdf

    its somewhat hasty creation (reportedly managed in only 10 days, in 1995, by Brendan Eich at Netscape), today it's a standardized and quickly growing language, with features more advanced than most ...

    Advanced R programming-By Hadley Wickham

    although you might not know all the details, and you should be somewhat familiar with the apply family of functions (like apply() and lapply()), although you may currently struggle to use them ...

    Mastering+JavaScript+Functional+Programming-Packt+Publishing(2017).epub

    Despite its somewhat hasty creation (reportedly managed in only 10 days, in 1995, by Brendan Eich at Netscape), today it's a standardized and quickly growing language, with features more advanced ...

    hydra-somewhat-advanced

    使用hydra的简单示例此示例没有对象实例化。这主要是main.py这很重要。输出示例$ python main.pyusing SVMsvm_cfg.kernel=<Kern>svm_cfg.C=1.0using Adult datasetadult_cfg.drop_native=Falsecfg.seed=42cfg.use_w...

    Advanced Metaprogramming in Classic C++, 3rd Edition

    Advanced Metaprogramming in Classic C++: Third Edition, offers a detailed and somewhat intense look into template metaprogramming (TMP) using Classic C++ code examples. The two newer standards are not...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to ...

    StatMentor

    Somewhat advanced show stats function, where you can see stats for just a single day or summarized for many days. No Access database ( just simple text files ) and no global.asa configuration. Just...

    How to be a Programmer

    This is very subjective and, therefore, this essay is doomed to be personal and somewhat opinionated. I confine myself to problems that a programmer is very likely to have to face in her work. Many of...

    Introduction to the Practice of Statistics

    It is elementary in mathematical level, but conceptually rich in statistical ideas and serious in its aim to help students think about data and use statistical methods with understanding. Although ...

    Microsoft_Press_eBook_Programming_Windows_8_Apps_with_HTML_CSS_and_JavaScript

    For the most part, then, I'm assuming that you're already at least somewhat conversant with these standards. We will cover some of the more salient areas like the CSS grid, which is central to app ...

    AOP in .NET

    To that end, this book is somewhat informal in tone and short on theory, and it contains lots of code samples, with which I hope you follow along. As much as I want this book to take a generalized ...

    Excel.Programming.Weeken.Crash.Course.rar

    cannot find a good source of information to guide them through the learning process. Excel programming can be somewhat complicated, which is unavoidable for such a powerful tool, but the truth is that...

    Cocoa and Objective-C: Up and Running (2010) 书及源代码

    You'll quickly gain the experience you need to develop sophisticated Apple software, whether you're somewhat new to programming or just new to this platform. Get a quick hands-on tour of basic ...

    Mathematica.Navigator 3e part2

    devoted to Mathematica as an advanced environment of writing mathematical documents. The online version of the book, which can be installed from the enclosed CD-ROM, makes the material easily ...

    Mathematica.Navigator 3e part1

    devoted to Mathematica as an advanced environment of writing mathematical documents. The online version of the book, which can be installed from the enclosed CD-ROM, makes the material easily ...

    ”Better Than Nothing” Privacy with Bloom Filters:To What Extent?.pdf

    Bloom filters are probabilistic data structures which permit to conve- niently represent set membership. Their performance/memory efficiency makes them appealing in a huge variety of scenarios. Their ...

Global site tag (gtag.js) - Google Analytics