在引用类的时候需要提前声明
eg: require(‘UIView,UIColor’)
类似于OC中类的实现
defineClass('ViewController', {
//在这里进行js动作
};
self.super()等于OC中的super
eg: [super viewDidLoad];
||
self.super().viewDidLoad();
结构体的使用(CGRect)
addRedView: function() {
var redView = UIView.alloc().initWithFrame({x:0, y:0, width:100, height:100});
redView.setCenter(self.view().center());
redView.setBackgroundColor(UIColor.redColor());
self.view().addSubview(redView);
定义对象方法
printNsstring: function(str) {
console.log("addRedView did run");
console.log(str);
},
定义类方法
defineClass('ViewController', {
//对象方法
},
{
//类方法 定义方式是一样的,只是位置不同
shared: function() {
console.log(self);
}
}
);
替换系统方法
替换viewDidLoad无效 根据生命周期来看 viewWillAppear由系统调用 在viewDidLoad之后 所以替换这个方法有效
viewWillAppear: function(animated) {
self.super().viewWillAppear(animated);
self.crashArray();
self.addRedView();
self.printNsstring("changed success");
//调用类方法
ViewController.shared();
},
注意:
利用转换工具 并不能将NSLog进行转换,输出时候还是需要用js语句输出 否则crash
JSPatchX(Xcode中编辑js代码补全插件):
https://github.com/bang590/JSPatchX
OC转换JS工具(官方)
http://bang590.github.io/JSPatchConvertor/
Demo:Github
参考文献:http://blog.csdn.net/u014410695/article/details/51151846