目前支持的选择器有:
选择器 | 样例 | 样例描述 |
---|
var number = 10; | |
console.log( "Number" === number.constructor ); | |
var string = "str"; | |
console.log( "String" === string.constructor ); | |
var boolean = true; | |
console.log( "Boolean" === boolean.constructor ); | |
var object = {}; | |
console.log( "Object" === object.constructor ); | |
var func = function(){}; | |
console.log( "Function" === func.constructor ); |
//生成 regexp 对象需要使用 getRegExp函数。 | |
getRegExp(pattern[, flags]) | |
//参数: | |
//pattern: 正则表达式的内容。 | |
//flags:修饰符。该字段只能包含以下字符: g: global ; i: ignoreCase ; m: multiline。 | |
// 示例代码: | |
var a = getRegExp("x", "img"); | |
console.log("x" === a.source); | |
console.log(true === a.global); | |
console.log(true === a.ignoreCase); |
//生成 date 对象需要使用 getDate函数, 返回一个当前时间的对象。 | |
getDate() | |
getDate(milliseconds)//milliseconds: 从1970年1月1日00:00:00 UTC开始计算的毫秒数 | |
getDate(datestring)//datestring: 日期字符串,其格式为:"month day, year hours:minutes:seconds" | |
getDate(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]]) | |
var date = getDate(); //返回当前时间对象 | |
date = getDate(1500000000000); | |
// Fri Jul 14 2017 10:40:00 GMT+0800 (中国标准时间) | |
date = getDate('2017-7-14'); | |
// Fri Jul 14 2017 00:00:00 GMT+0800 (中国标准时间) |
var a = function(){ | |
console.log(3 === arguments.length); | |
console.log(100 === arguments[0]); | |
console.log(200 === arguments[1]); | |
console.log(300 === arguments[2]); | |
}; | |
a(100,200,300); |
//方法 1 | |
function a (x) { | |
return x; | |
} | |
//方法 2 | |
var b = function (x) { | |
return x; | |
} | |
//function 同时也支持以下的语法(匿名函数,闭包等): | |
var a = function (x) { |
var o = {} //生成一个新的空对象 | |
//生成一个新的非空对象 | |
o = { | |
'string' : 1, //object 的 key 可以是字符串 | |
const_var : 2, //object 的 key 也可以是符合变量定义规则的标识符 | |
func : {}, //object 的 value 可以是任何类型 | |
}; | |
//对象属性的读操作 | |
console.log(1 === o['string']); | |
console.log(2 === o.const_var); |
import wepy from 'wepy'; | |
// export default class MyPage extends wepy.page { | |
export default class MyComponent extends wepy.component { | |
customData = {} // 自定义数据 | |
customFunction () {} //自定义方法 | |
onLoad () {} // 在Page和Component共用的生命周期函数 | |
onShow () {} // 只在Page中存在的页面生命周期函数 | |
config = {}; // 只在Page实例中存在的配置数据,对应于原生的page.json文件 | |
data = {}; // 页面所需数据均需在这里声明,可用于模板数据绑定 | |
components = {}; // 声明页面中所引用的组件,或声明组件中所引用的子组件 |
import wepy from 'wepy'; | |
// 声明一个App小程序实例 | |
export default class MyAPP extends wepy.app { | |
} | |
// 声明一个Page页面实例 | |
export default class IndexPage extends wepy.page { | |
} | |
// 声明一个Component组件实例 | |
export default class MyComponent extends wepy.component { | |
} |
<!--wxml--> | |
<!-- 下面的 getMax 函数,接受一个数组,且返回数组中最大的元素的值 --> | |
<wxs module="m1"> | |
var getMax = function(array) { | |
var max = undefined; | |
for (var i = 0; i < array.length; ++i) { | |
max = max === undefined ? | |
array[i] : | |
(max >= array[i] ? max : array[i]); | |
} |